Thread: why big numbers change in floating-point variables?

  1. #1
    Registered User
    Join Date
    Nov 2016
    Posts
    18

    why big numbers change in floating-point variables?

    hi i don't know why but some of the values change on their own when i run this code. why?
    Code:
    #include <stdio.h>
    #include <math.h>
    
    int main(void)
    {
        double a = 111 * pow(10,20);
        double b = 128 * pow(10,20);
        double c = 255 * pow(10,20);
        double d = 256 * pow(10,20);
        double e = 10 * pow(10,20);
    
        printf("%f\n%f\n%f\n%f\n%f\n", a,b,c,d,e);
        return 0;
    }
    my output:

    Code:
    11100000000000001048576.000000
    12800000000000000000000.000000
    25500000000000001048576.000000
    25600000000000000000000.000000
    1000000000000000000000.000000
    if you look at the output the first and third value changed on their own.

  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
    See Floating-point arithmetic - Wikipedia
    You have to understand that all floats are approximations, with a limited number of digits of precision.

    See also, <float.h>
    Specifically DBL_DIG = 10

    What this means is that only the left-most 10 digits of the number are accurate. The rest are noise.

    11100000000000001048576.000000
    12800000000000000000000.000000
    25500000000000001048576.000000
    25600000000000000000000.000000
    1000000000000000000000.000000
    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.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. floating point numbers
    By MK27 in forum C Programming
    Replies: 2
    Last Post: 07-14-2008, 06:39 PM
  2. Floating-Point Numbers
    By MindLess in forum C Programming
    Replies: 4
    Last Post: 06-24-2007, 11:45 PM
  3. floating point variables in edittext controls
    By dootickle in forum Windows Programming
    Replies: 3
    Last Post: 04-15-2004, 11:15 AM
  4. Floating point numbers
    By noor_mirza in forum C++ Programming
    Replies: 3
    Last Post: 10-23-2002, 03:40 AM
  5. Floating point numbers
    By bulsquare in forum C Programming
    Replies: 2
    Last Post: 04-10-2002, 04:44 AM

Tags for this Thread