Thread: floating point numbers

  1. #1
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300

    floating point numbers

    Why does this:

    Code:
    #include <stdlib.h>
    #include <stdio.h>
    
    int main () {
            float this=20000.6;
            printf ("%.5f\n", this);
    }
    give me this output:

    20000.59961
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  2. #2
    Woof, woof! zacs7's Avatar
    Join Date
    Mar 2007
    Location
    Australia
    Posts
    3,459
    Because that's what it's supposed to do. See the manual. http://www.cplusplus.com/reference/c...io/printf.html

    It's 5 digits after the decimal place, as per the manual. Not to mention floating points are approximations hence 20000.6 ~= 20000.59961
    Last edited by zacs7; 07-14-2008 at 06:14 PM.

  3. #3
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    For more information about IEEE floats (which you're almost certainly using) look here.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. For the numerical recipes in C types!
    By Smattacus in forum C Programming
    Replies: 5
    Last Post: 10-28-2008, 07:57 PM
  2. Replies: 7
    Last Post: 12-02-2007, 05:55 AM
  3. floating point operators
    By DavidP in forum A Brief History of Cprogramming.com
    Replies: 15
    Last Post: 10-22-2003, 07:53 PM
  4. 2 questions about floating point and %
    By ams80 in forum C Programming
    Replies: 2
    Last Post: 08-14-2002, 10:55 AM
  5. Floating point faster than fixed-point
    By VirtualAce in forum A Brief History of Cprogramming.com
    Replies: 5
    Last Post: 11-08-2001, 11:34 PM

Tags for this Thread