Thread: simple mathematical expression with mixed data types

  1. #1
    Learner Axel's Avatar
    Join Date
    Aug 2005
    Posts
    335

    simple mathematical expression with mixed data types

    I can't seem to figure out the following expression with mixed data types:
    Code:
     
       int i = 10, j = 7;
       unsigned int k;
       double q = 3.56;
       char c;
    
    k = q * i - (2 * i) / j;
    printf("k = %d\n", k);

    c evaluates this to:

    33 i re-checked using googles calculator and i'm getting

    (3.56 * 10) - ((2 * i) / 7) = 35.6 - 0.285714286 i

    Is it because it's rounded off somehow ?

    next:

    Code:
       c = q * i * j;
       printf("c = %d\n", c);
    this is even more confusing correct me if i'm wrong the answer should be:

    3.56 * 10 * 7 = 249.2

    but C again, evaluates it to -7

    finally,

    Code:
       k = -(i * q);
       printf("k = %d\n", k);
    All these expressions are in the main function so "k" stores the previously evaluated expression 33

    so 33 - (10 * 3.56) = -2.6

    but C, again does not seem to agree:

    k = -35


  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    http://cboard.cprogramming.com/showthread.php?t=68647
    On the difference between / when applied to integers and floats.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    Registered User cbastard's Avatar
    Join Date
    Jul 2005
    Location
    India
    Posts
    167
    >>When an arithmatic operation is applied between two or more variable compiler upgrades the type of all variables to the largest type.when operation is between double and int,it changes i to double and find the value 35.6.the value of int 2*i/j is 2.subtracting we get 33.56 but it is stored as unsigned int so value is 33.
    >>q*i*j will be treated as double but when it is stored in character whose value ranges -128 to 127 thevalue shows -7

  4. #4
    Learner Axel's Avatar
    Join Date
    Aug 2005
    Posts
    335
    I'm still having trouble understanding it :| even after reading the other thread.

    What about k = -(i * q);

    "upgrades the type of all variables to the largest type.when operation is between double and int,it changes i to double and find the value"

    it makes no difference i tried changing the variables to double but i still get the wrong result:

    3.56 * 10.0 * 7.0 = 249.2

    i must of missed something

  5. #5
    Registered User cbastard's Avatar
    Join Date
    Jul 2005
    Location
    India
    Posts
    167
    Quote Originally Posted by Axel
    I'm still having trouble understanding it :| even after reading the other thread.


    it makes no difference i tried changing the variables to double but i still get the wrong result:

    3.56 * 10.0 * 7.0 = 249.2

    i must of missed something
    In 3.56*10.0*7.0.You dont need to change the variable to double,compiler wil automatically do that for you.You need to change the char variable c.because the value a char can store is between -128 to 127.but you are assigning value different than this so value which is stored is not desirable.you need to define variable c as int or double(for more precision).

    What about k = -(i * q);
    You are assigning a new value to k.so the value stored in k is -i*q.
    but i think you want to store previous value of k -i*q.
    for that expression is k-=i*q not k=-i*q.
    I hope you will get it now

  6. #6
    Learner Axel's Avatar
    Join Date
    Aug 2005
    Posts
    335
    I appreciate the time you've taken to answer my question but unfornately i still don't understand the logic behind it, even after playing around with the variables and changing it.

    the following statement:

    k = q * i - (2 * i) / j;

    equals to 33

    the last expression, when i minus i*q (35.6) i get 2.6. but the final answer of k should be -33 :confused. Anyway i think i need to first statement k = q * i - (2 * i) / j; in order to understand the last one.

    ">>q*i*j will be treated as double but when it is stored in character whose value ranges -128 to 127"

    i tried changing char to int and i get the correct answer, but once it's char i just get -7. How do i find the value of 249.0 (assuming it's being treated as a double) that ranges between -128 to 127?


    I've tried breaking down the problem into smaller bits i.e. doing part of the expression but it still doesn't add up? Could you possibly (if you get time) explain the steps you took into reaching the final answer. i think substituting the variable to the actual number would help?

  7. #7
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    but the final answer of k should be -33
    No it shouldn't. Everyone seems to have missed one obvious point:
    unsigned int k;
    How can you expect k to be negative?

    As to why you get 33, walk through your origional post:
    int i = 10, j = 7;
    unsigned int k;
    double q = 3.56;
    char c;

    k = q * i - (2 * i) / j;
    printf("k = %d\n", k);
    1) Make all subsitutions. Thus:
    Code:
    k = 3.56 * 10 - (2 * 10) / 7;
    2) 2 * 10 = 20. Thus:
    Code:
    k = 3.56 * 10 - 20 / 7;
    3) 3.56 * 10 = 35.6. Thus:
    Code:
    k = 35.6 - 20 / 7;
    4) 20 / 7 = 2. Thus:
    Code:
    k = 35.6 - 2;
    5) Final result:
    Code:
    k = 33;
    Exactly what it should be. Why in fact...
    Code:
    #include<stdio.h>
    int main( void )
    {
        int i = 10, j = 7;
        unsigned int k;
        double q = 3.56;
    
        k = q * i - (2 * i) / j;
        printf("k = %d\n", k);
    
    	return 0;
    }
    /*
    gcc -o maths maths.c -Wall -pedantic -ansi
    ./maths
    k = 33
    */
    That's exactly what it is.

    On your further point, you really all did miss the boat. k is as stated, unsigned. No matter what you do to it, it'll always be positive.


    Quzah.
    Hope is the first step on the road to disappointment.

  8. #8
    Registered User cbastard's Avatar
    Join Date
    Jul 2005
    Location
    India
    Posts
    167
    dear i would like to expalin that thing but am busy for 2-3 days(caz of festival in india) after that i will expalin you this thing .

  9. #9
    Learner Axel's Avatar
    Join Date
    Aug 2005
    Posts
    335
    thanks, quzah i understand the first bit now. The 2nd and 3rd expression still seem to confuse me. After a bit of research i've slighly made some progress but a question is still left unanswered:

    c = q * i * j;

    c is a char, when the expression is evaluated it is stored as a char value between -127 and 128 since 249 is out of range the char value is outputted. I'm trying to find how to get that value, if you convert 249 to binary:
    11111001
    you get 6 bytes, i think it's still wrong because the answer is -7.


    Here's something intresting:


    c = 35

    when the values are double q = 3.56; int i = 10, j = 1;

    c = 71

    when the values are double q = 3.56; int i = 10, j = 2;

    so far so good:

    c = 106

    when the values are double q = 3.56; int i = 10, j = 3;




    c = -114

    when the values are double q = 3.56; int i = 10, j = 4;

    ... why is it starting give a negative number?


    c = -78

    when the values are double q = 3.56; int i = 10, j = 5;



    is it some sort of overflow?

    I'm also confused with the following although i do understand logic but why is the result affected when the += and are changed around? i.e:


    k = -(i * q);

    gives k = -35





    k - =(i*q);

    gives k = -2


    I tried the exact same thing with the following:



    int one = 1;
    int two = 2;
    int total = 0;


    total -= (one*two);

    total = -2

    and

    total =- (one*two);

    total = -2

    as you can see i got the same result regardless of the position that i put the = and -
    Last edited by Axel; 08-19-2005 at 06:13 AM.

  10. #10
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Quote Originally Posted by Axel
    i've slighly made some progress but a question is still left unanswered:

    c = q * i * j;

    c is a char, when the expression is evaluated it is stored as a char value between -127 and 128 since
    Quote Originally Posted by Axel
    Here's something intresting:
    c = 35
    when the values are double q = 3.56; int i = 10, j = 1;

    c = 71
    when the values are double q = 3.56; int i = 10, j = 2;

    so far so good:
    c = 106
    when the values are double q = 3.56; int i = 10, j = 3;

    c = -114
    when the values are double q = 3.56; int i = 10, j = 4;
    ... why is it starting give a negative number?
    See above. You've already answered your own question. 142 is too big to fit in something with a maximum of 128.
    Quote Originally Posted by Axel
    c = -78
    when the values are double q = 3.56; int i = 10, j = 5;

    is it some sort of overflow?
    Yep. You're overflowing your char.

    Quote Originally Posted by Axel
    I'm also confused with the following although i do understand logic but why is the result affected when the += and are changed around? i.e:

    k = -(i * q);

    gives k = -35

    k - =(i*q);

    gives k = -2
    What is the initial value of k? The first one, you are making an assignment. "Assign netagive (i * q) to k." The second you are making a subtraction. "Subtract (i * q) from whatever k already is."

    Quote Originally Posted by Axel
    I tried the exact same thing with the following:
    int one = 1;
    int two = 2;
    int total = 0;

    total -= (one*two);

    total = -2

    and

    total =- (one*two);

    total = -2

    as you can see i got the same result regardless of the position that i put the = and -
    They're the same here because you have set 'total' to zero. Thus, the end result just happens to be the same. Try setting 'k' to zero in the above example each time, and you'll find it works the same also.


    Quzah.
    Hope is the first step on the road to disappointment.

  11. #11
    Learner Axel's Avatar
    Join Date
    Aug 2005
    Posts
    335
    Thanks again, i understand it now but theres just one thing i'm curious about:

    Quote Originally Posted by quzah
    See above. You've already answered your own question. 142 is too big to fit in something with a maximum of 128.


    Quzah.
    Why is c = -114? why not -120 etc? and why does the negetive number increase as it overflows ? i still haven't been able to calculate it it overflows by 14 in the first instance,

    when the values are double q = 3.56; int i = 10, j = 4;

    in the second instance,

    when the values are double q = 3.56; int i = 10, j = 5;

    it overflows by 50 giving the answer -78.

    I guess you can ignore the question about why it increases simply because that is how overflows are handled with chars.

  12. #12
    Learner Axel's Avatar
    Join Date
    Aug 2005
    Posts
    335
    never mind i worked out the answer, for those who are curious:

    the overflow limit is 128 if you subtract 14 thus -114 simply because we went over 128 by 14.



    the original expression,

    3.56 * 10 * 7 = 249.2

    evaluated to: 249.2

    121 because it overflowed by that amount thus:


    128 - 121 = 7

    -7

    i hope i'm right anyway
    Last edited by Axel; 08-20-2005 at 06:01 AM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 48
    Last Post: 09-26-2008, 03:45 AM
  2. problem reading mixed data...
    By coralreef in forum C Programming
    Replies: 3
    Last Post: 11-08-2006, 05:29 PM
  3. Errors
    By Rhidian in forum C Programming
    Replies: 10
    Last Post: 04-04-2005, 12:22 PM
  4. Data types in Unicode
    By Garfield in forum Windows Programming
    Replies: 12
    Last Post: 10-28-2001, 10:48 AM
  5. Need help with simple data types
    By partnole in forum C++ Programming
    Replies: 1
    Last Post: 10-03-2001, 08:36 AM