Thread: Confusion regarding the storage of floating numbers on various architectures

  1. #1
    Registered User
    Join Date
    Nov 2008
    Posts
    2

    Confusion regarding the storage of floating numbers on various architectures

    Hi,
    consider following 2 programmes:

    Programme 1:
    Code:
    int main(){
        printf("%f\n",7/2);
        return 0;
    }
    Programme 2:

    Code:
    int main(){
        int a = 7;
        int b = 2;
        printf("%f\n",a/b);
        return 0;
    }
    Programme 3:

    Code:
    int main(){
        int a = 7;
        printf("%f\n",7/2);
        return 0;
    }
    I am getting different outputs in programme 1 and programme2 . Output of Programme 3 is same as that of 2.

    I expected the output of these programmes to be machine dependant as it depends on how a floating point number is stored.
    But I am unable to understand why the output of programme 1 and programme 2 is different? Both the programmes actually do the same thing. Has it something to do with the organization of the programme as well?

    I am using a debian machine and the normal gcc compiler.

    Regards,
    Pratik

  2. #2
    Registered User cph's Avatar
    Join Date
    Sep 2008
    Location
    Indonesia
    Posts
    86
    maybe you should consider trying this
    Code:
    printf("%f\n", 7.0 / 2);
    or this
    Code:
    printf("%f\n", 7 / 2.0);
    or this
    Code:
    printf("%f\n", 7.0 / 2.0);
    or this
    Code:
    float a = 7.0;
    float b = 2.0;
    printf("%f\n", a / b);

  3. #3
    Registered User
    Join Date
    Aug 2008
    Location
    Belgrade, Serbia
    Posts
    163
    Casting the variables to float and putting a .0 after a literal number will force all values to be float so it should give the same results.

    Code:
    int main(){
        printf("%f\n",7.0/2.0);
        return 0;
    }
    Code:
    int main(){
        int a = 7;
        int b = 2;
        printf("%f\n",(float)a/(float)b);
        return 0;
    }
    Code:
    int main(){
        int a = 7; //What's the point of this,
        printf("%f\n",7.0/2.0); //if you don't use it?
        return 0;
    }
    Vanity of vanities, saith the Preacher, vanity of vanities; all is vanity.
    What profit hath a man of all his labour which he taketh under the sun?
    All the rivers run into the sea; yet the sea is not full; unto the place from whence the rivers come, thither they return again.
    For in much wisdom is much grief: and he that increaseth knowledge increaseth sorrow.

  4. #4
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    And exactly what output do you get from that - I'd expect that you'd get some pretty rubbish results, as you are printing an integer value as a floating point result. Since you are passing only an integer, half the result of your printout will depend on the content of where the other half of the double that the printf function expects. This is not at all clearly defined, and may be one of your integer values or some completely different random data - so yes, I expect that different bits of code would produce different results, but not in any way predictably - change the code around to printf("Hello, World\n") before the printf of the float and you'd get a different results. Add some calculations, change the compiler options, compile with a different compiler, etc, etc, and each time you change anything, it will change the results in some way [probably - there may be combinations that do not change the results - it is not easy to predict].

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  5. #5
    Woof, woof! zacs7's Avatar
    Join Date
    Mar 2007
    Location
    Australia
    Posts
    3,459
    Actually, "7.0" is a double constant, "7.0f" is the float constant equivalent. Important, but not really at the same time

    so, 7.0/2.0 results in a double.

    Also, as far as I know there's nothing stopping certain architectures from rounding instead of truncating integer division? But that's not the problem here.
    Last edited by zacs7; 11-11-2008 at 05:05 AM.

  6. #6
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by zacs7 View Post
    Actually, "7.0" is a double constant, "7.0f" is the float constant equivalent. Important, but not really at the same time

    so, 7.0/2.0 results in a double.
    And any double passed a variable arguments function will be converted to double by standard, so using double or float in the above code will give nearly identical results.
    Also, as far as I know there's nothing stopping certain architectures from rounding instead of truncating integer division? But that's not the problem here.
    Certainly not part of the problem here, but I'm pretty darn sure that the specification for integer division in C is that it truncates - a lot of code using division to split numbers into portions would fail if that was the case, and whilst most of us will not run on obscure machines, even the code in glibc that produces numbers for printf and such does use a iteration of divide by base to get the individual digits out of a number.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  7. #7
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by matsp
    I'm pretty darn sure that the specification for integer division in C is that it truncates
    Yes, it does: "When integers are divided, the result of the / operator is the algebraic quotient with any fractional part discarded." A note at the end of that sentence says that: 'This is often called "truncation toward zero"'.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  8. #8
    Woof, woof! zacs7's Avatar
    Join Date
    Mar 2007
    Location
    Australia
    Posts
    3,459
    wow thanks laserlight and matsp, I've always wondered if it was in the standard. If not that'd certainly be a real hassle if you ever had to work with a machine like that.

    Sorry for hijacking the thread

  9. #9
    Registered User
    Join Date
    Nov 2008
    Posts
    2
    Thanks Mats,
    I got my answer.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. 2 questions about floating point and %
    By ams80 in forum C Programming
    Replies: 2
    Last Post: 08-14-2002, 10:55 AM
  2. floating point numbers display something wrong i think
    By Unregistered in forum C Programming
    Replies: 3
    Last Post: 04-27-2002, 03:18 AM
  3. Floating point numbers
    By bulsquare in forum C Programming
    Replies: 2
    Last Post: 04-10-2002, 04:44 AM
  4. Very large floating point numbers
    By bulsquare in forum C Programming
    Replies: 0
    Last Post: 04-08-2002, 04:10 PM
  5. A (complex) question on numbers
    By Unregistered in forum C++ Programming
    Replies: 8
    Last Post: 02-03-2002, 06:38 PM

Tags for this Thread