Thread: Float variable in C acting strange

  1. #1
    Registered User
    Join Date
    May 2010
    Posts
    35

    Float variable in C acting strange

    I'm trying to do something for an assignment and we have to use a float variable. I was getting really messed up results and I couldn't figure out why so I wrote this program just to test some stuff and the results I don't understand.

    Code:
    #include <stdio.h>
    
    int main()
    {
       float f;
       scanf("%f", &f);
       printf("%.2f\n", f);
       return 0;
    }
    If I enter in a small number say 45 the output is 45.00 which is correct. Same as something like 999.99 it prints 999.99

    If I enter larger numbers (well within the float value range), say 123456789 it prints 123456792.00 (92 at the end huh?) Or something like 12345678901234567890 it prints 12345679395506094080.00 which is totally off. I'm just using ANSI C any ideas what is going on ?

    According to MSDN Float in C should be able to go from 1.175494351 E – 38 to 3.402823466 E + 38 so my numbers easily within those values.

  2. #2
    Password:
    Join Date
    Dec 2009
    Location
    NC
    Posts
    587
    Just a guess: Maybe there's a bug in the implementation of libc you're using.

  3. #3
    ... kermit's Avatar
    Join Date
    Jan 2003
    Posts
    1,534
    Quote Originally Posted by fxtdr79 View Post
    According to MSDN Float in C should be able to go from 1.175494351 E – 38 to 3.402823466 E + 38 so my numbers easily within those values.
    A single precision float on the machine I am working on right now (i.e, 32 bit) is 4 bytes, or 32 bits. An unsigned integer is also 4 bytes, with a maximum value of 4294967295 - which is a ten digit number. One would think that the 10 digits of a 4 byte int would fit into a a 4 byte float, but such is not the case. You cannot properly represent more than about 7 digits with a single precision float (in other words, if you put a 10 digit int in, you will not get the same number out via the float). If you want to represent more digits, you need to use a double.
    Last edited by kermit; 07-04-2010 at 08:23 PM.

  4. #4
    Registered User
    Join Date
    May 2010
    Posts
    35
    Thanks, that seems to be the exact issue I was wondering about.

  5. #5

  6. #6
    Registered User
    Join Date
    May 2003
    Posts
    1,619
    Think of a float like scientific notation, e.g. you could write 1.235x10^3 or 7.456x10^20. Both numbers have 4 significant figures.

    A single-precision float stores 24 significant figures if you wrote it in binary. In other words, if you converted the value to binary, then got rid of any leading zeroes, the highest 24 bits are stored. So you can think of it like converting the number to binary, storing the position of the first '1' bit, and then storing the following 23 bits, throwing out all bits beyond that point.

    A double precision float effectively keeps 53 significant bits.
    You ever try a pink golf ball, Wally? Why, the wind shear on a pink ball alone can take the head clean off a 90 pound midget at 300 yards.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Model Rocket Altitude predictor...
    By kalor_alros in forum C++ Programming
    Replies: 11
    Last Post: 09-04-2009, 12:27 AM
  2. Moving Average Question
    By GCNDoug in forum C Programming
    Replies: 4
    Last Post: 04-23-2007, 11:05 PM
  3. Debug Error Really Quick Question
    By GCNDoug in forum C Programming
    Replies: 1
    Last Post: 04-23-2007, 12:05 PM
  4. Possible Loss of data
    By silicon in forum C Programming
    Replies: 3
    Last Post: 03-24-2004, 12:25 PM
  5. ~ Array Help ~
    By Halo in forum C++ Programming
    Replies: 1
    Last Post: 11-08-2002, 04:19 PM