Thread: The case of missing '1'

  1. #1
    Registered User culater's Avatar
    Join Date
    May 2015
    Posts
    8

    The case of missing '1'

    Hi there,

    I've made a basic code for solving exponents:
    Code:
    #include <stdio.h>
    #include <math.h>
    /*exponent*/
    int main ()
    {
    int a,b,c;
    printf("Format: a^b\n");
    printf("Enter a and b\n");
    scanf("%d%d", &a, &b);
    c = exp(b*log(a));
    c = c+1; /*WHY???*/
    printf("%d", c);
    return 0;
    }
    When I run it, strangely the value of output is less by '1' than the correct one. Now this can be easily taken care of by the 'c = c+1' equation. But, where did that '1' go?

  2. #2
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,612
    Aren't you just using the wrong function?
    a^b is pow(a, b);
    exp(x); is e^x.

  3. #3
    Registered User culater's Avatar
    Join Date
    May 2015
    Posts
    8
    Not really, I'm making use of the following properties:
    The case of missing '1'-untitled3-png:
    Here log represents natural logarithm which has the base 'e'.
    By the way, thanks. I was not aware of the 'pow' function.
    Last edited by culater; 05-30-2015 at 06:49 AM.

  4. #4
    Registered User
    Join Date
    Jun 2011
    Posts
    4,513
    It works for me ... in that, when I run it, the result is one higher than it should be due to your "correction". What input/output are you seeing?

  5. #5
    Registered User culater's Avatar
    Join Date
    May 2015
    Posts
    8
    Maybe there is some bug in my compiler, Matticus.
    Here's a snip of the input without the correction with the respective output:
    The case of missing '1'-capture2-png
    I am using jGRASP version 2.0.1_01.

  6. #6
    Ticked and off
    Join Date
    Oct 2011
    Location
    La-la land
    Posts
    1,728
    Quote Originally Posted by culater View Post
    Code:
    int a,b,c;
    c = exp(b*log(a));
    The parameters to exp() and log() are doubles, and the results are doubles, too.

    Because you use the int type, the compiler promotes the parameters a and b to double automatically (according to standard C rules, that is), then truncates the result to an integer when storing to c, exactly as it should.

    You seem to expect different behaviour, and therefore added the c = c+1; /*WHY???*/ line. That is your error.

    If you need correct rounding for printing only -- and rounding is the correct thing to do here, since we are dealing with floating-point precision values here --, use
    Code:
    printf("%.0f\n", exp( (double)b * log( (double)a ));
    The (double)b and (double)a are explicit casts. They are technically optional, but recommended: having them there tells both the compiler and the human reader that the int parameters should be cast to double type before being operated on.

    In practice, I'd declare a, b, and c as double, and use
    Code:
        if (a < 0.0)
            c = exp(b * log(-a));
        else
        if (a > 0.0)
            c = exp(b * log(a));
        else
        if (b == 0.0)
            c = 1.0;
        else
            c = 0.0;
        printf("%.0f\n", c);
    since there is no practical reason to use int here at all, and your implementation would choke at nonpositive a. The last two clauses implement 00 = 1, 0b = 0 for all b ≠ 0; you might wish to modify them. (In particular, typically 00=1, 0b=0 for all b>0, and 0b is undefined for all b<0, so you might wish to insert a last if clause to print an error message instead if b is negative.)

    The scanf() pattern for a double is %lf.

  7. #7
    Registered User culater's Avatar
    Join Date
    May 2015
    Posts
    8
    Thanks a lot for your time and patience Nominal Animal! The code runs smoothly now. I will keep this concept in mind.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Upper case= lower case
    By ashley1nonly in forum Tech Board
    Replies: 3
    Last Post: 02-19-2015, 12:52 AM
  2. Upper case= lower case
    By ashley1nonly in forum C Programming
    Replies: 0
    Last Post: 02-18-2015, 10:24 PM
  3. Replies: 11
    Last Post: 08-25-2008, 12:01 PM
  4. upper case to lower case problem
    By Jasonymk in forum C++ Programming
    Replies: 3
    Last Post: 04-27-2003, 05:35 AM
  5. something is missing, and i just cant see it!!
    By Alicia in forum C Programming
    Replies: 2
    Last Post: 01-31-2002, 06:42 PM

Tags for this Thread