Thread: problems w/my simple function

Threaded View

Previous Post Previous Post   Next Post Next Post
  1. #7
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    Quote Originally Posted by generalt View Post
    So, if I only read in 2 decimal places that would solve the problem of imprecision? In this program, I don't think I'm in trouble w/imprecision b/c I convert it into an integer... right?
    How did you plan to "read in only 2 decimal places"? Nb. That the problem *is* with converting floats into integers. There is a very long thread here if you want to look at it:
    float13

    If you want a quick illustration, try this:
    Code:
    #include <stdio.h>
    
    int main () {
    	int X=1,i;
    	float x;
    	for (i=0;i<25;i++) {
    		x=(float)X/10;
    		printf("%d %f %d\t",X,x,i);
    		x+=0.1f;
    		X=x*10;
    		printf("%d %f \n",X,x);
    	}
    	return 0;			
    }
    On a 32-bit system, you will notice something strange happen starting at 13. On a 64-bit system, it starts at 21.

    Understand that binary floating point arithmetic is really something almost unique to computers, and not to confuse the concept with decimal numbers. You should deal with money as an integer (the number of cents), because that is what money is. Using a float is 1) inappropriate, 2) prone to error.
    Last edited by MK27; 05-08-2009 at 08:41 AM.
    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

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Including lib in a lib
    By bibiteinfo in forum C++ Programming
    Replies: 0
    Last Post: 02-07-2006, 02:28 PM
  2. Game Pointer Trouble?
    By Drahcir in forum C Programming
    Replies: 8
    Last Post: 02-04-2006, 02:53 AM
  3. <Gulp>
    By kryptkat in forum Windows Programming
    Replies: 7
    Last Post: 01-14-2006, 01:03 PM
  4. Binary Search Trees Part III
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 16
    Last Post: 10-02-2004, 03:00 PM
  5. Big problems with the Text function
    By GaPe in forum C Programming
    Replies: 26
    Last Post: 05-22-2002, 10:42 AM