Re: C floating point

From: John Bode (john_bode_at_my-deja.com)
Date: 03/01/04


Date: 1 Mar 2004 13:41:33 -0800

j0mbolar@engineer.com (j0mbolar) wrote in message news:<2d31a9f9.0403010646.630174ed@posting.google.com>...
> C supports single precision floating point and double precision
> floating point but does it support fixed floating point? i've read
> that fixed floating point is more accurate than single precision
> floating point when dealing with dollars and cents.

C doesn't have a built-in fixed data type. What you can do is use an
integral type, scaled to the smallest unit. For example, the integer
12345 may represent $123.45 (counting in cents), or $12.345 (counting
in tenths of cents), or $1.2345 (counting in hundredths of cents).

As you can see, the more precise the unit, the smaller the range. An
unsigned 16-bit integer can count from 0 to 65,535; if you're counting
in hundredths of cents, that gives you a range of $0 to $6.5535.

Alternately, you can use a struct with two members, one to keep track
of whole dollar amounts, the other to keep track of fractional
amounts:

struct val {
    int dollar;
    int fract;
};

so that the value $12.3456 is stored as {12, 3456}. This way you can
represent a larger range of dollars, yet still keep the precision you
want; unfortunately, the arithmetic gets a little hairier. Think
about adding the values $12.3456 and $45.82; you'll have to perform
some kind of addition on the struct values {12, 3456} and {45, 8200}.

You could also represent values as arrays of int, where each array
element corresponds to a digit (i.e., the value 123456789 is
represented as int val[]={1,2,3,4,5,6,7,8,9};); this way you can
represent values with tens or hundreds of significant digits. Again,
the math operations aren't as straightforward as for built-in types.



Relevant Pages

  • Re: gcc strangeness
    ... binary floating point arithmetic, rather than gcc. ... The correct solution is to use rint(3), ... Switching to double precision will help but is still not a complete ...
    (freebsd-hackers)
  • Re: double is integer?
    ... safe thing to do ... floating point computations, you may have a value close to but not ... precision anyway. ... long int i = d; ...
    (microsoft.public.vc.mfc)
  • Re: Problem in Float Arithmetic
    ... precision of an integer of the same number of bits of significance as the mantissa of the ... The problem, for example, is that 'float' has fewer bits than ... 'int', so it only becomes interesting when you start using 'double'. ... Floating point arithmetic on integer values that produces integer values is ...
    (microsoft.public.vc.mfc)
  • Re: how to use recursive algorithm to determine all of the arrangements?
    ... The simplest way is to check for integer overflow is to do the multiplication in floating point precision, and check if the result is larger than what would fit in whatever primitive type you're trying to store the value in. ... In the case of int, you could use long instead of double. ...
    (comp.lang.java.programmer)
  • Re: Precision
    ... Whether one does the subsequent calculations in internal ie integer format ... I have always used external format with Precision 4 since my ... floating point calcs which are done in binary have around 3% error albeit at ... positions it is IMPOSIBLE to have a result of 4 decimals with the 2 ...
    (comp.databases.pick)