From: Tim Slattery on 15 Sep 2009 12:45 "Frank Cisco" <tdyjkdftyujdtjyj(a)dtyjdtyjdtyjdty.com> wrote: >How do you get an accurate addition of a float and an int? > >int i = 5454697; >float f = 0.7388774F; > >float result = new Integer(i).floatValue()+f; > >result=5454697.5 ?? > >Surely it should be 5454697.7388774? If I use double it's fine, but I need >to use float You looking for an answer with 14 (decimal) digits of precision. A single-precision floating point number uses 23 bits for its mantissa, that works out to 6 or 7 decimal digits of precision. You simply cannot get the precision you want without using doubles. A double has a 52 bit mantissa, that's something like 15 decimal digits. -- Tim Slattery Slattery_T(a)bls.gov http://members.cox.net/slatteryt
From: Lew on 21 Sep 2009 08:30 Arne Vajhøj <arne(a)vajhoej.dk> wrote: >> 24*log10(2) is a bit more than 6. Tim Slattery wrote: > Huh?? A single-precision float has a 23-bit mantissa. 2^23 is > 8,388,608. That's seven digits, but only six that you can run all the > way from 0 to 9. Actually, since the leading '1' is not explicit in the mantissa, it holds 24 bits of information for non-denorm floats, not 23. -- Lew
From: Patricia Shanahan on 21 Sep 2009 10:16 Tim Slattery wrote: > Arne Vajh�j <arne(a)vajhoej.dk> wrote: > >> Roedy Green wrote: >>> On Tue, 15 Sep 2009 17:17:40 +0100, "Frank Cisco" >>> <tdyjkdftyujdtjyj(a)dtyjdtyjdtyjdty.com> wrote, quoted or indirectly >>> quoted someone who said : >>>> Surely it should be 5454697.7388774? If I use double it's fine, but I need >>>> to use float >>> see http://mindprod.com/jgloss/floatingpoint.html >>> >>> floats are only accurate to about 6 significant digits. >> 24*log10(2) is a bit more than 6. > > Huh?? A single-precision float has a 23-bit mantissa. 2^23 is > 8,388,608. That's seven digits, but only six that you can run all the > way from 0 to 9. > There's a special trick that only applies to normalized binary floating point. Think about the leading mantissa digit. It is a binary digit, and because of normalization it cannot be zero. In the relevant range, IEEE 754 floats are normalized, so for purposes of precision the 32 bit float mantissa is 24 bits, but only 23 bits of it need to be stored. Patricia
From: Tom Anderson on 21 Sep 2009 18:17 On Tue, 15 Sep 2009, Daniel Pitts wrote: > Mike Schilling wrote: >> Patricia Shanahan wrote: >>> Frank Cisco wrote: >>>> cheers all! why on earth is float used at all then if it's so >>>> inaccurate? >>> There are situations in which it is very useful. >>> >>> For example, some years ago I was doing some customer support work at >>> a geophysical consultancy. I saw they were doing some massive >>> calculations in float, and asked about it. >>> >>> They had a numerical algorithms expert on staff who knew far more than >>> I'll ever know about numerical analysis and algorithm stability. Their >>> input data, seismic traces, was inherently imprecise. Similarly, they >>> only needed a few decimal digits in the output. The algorithms expert >>> had decided that, given the algorithms they were using, they would get >>> the digits they needed using float. Benchmarking showed a significant >>> performance difference, because using float halved the data volume. >>> >>> If they had used double, they would have spent a lot of resources >>> storing and moving bits that were meaningless. >> >> Note to self: double is ideal for analyzing marketing data. > > Bzzt... wrong. > "Ideal" would probably be a Rational ("a/b"), or BigDecimal. Nlek-nerr! For marketing data, you want java.util.Random. tom -- Beware! Inside pie, there are chickens, gamecubes, Moltres, Raichu, and bacteria(in a good way!) -- Wikipedia
First
|
Prev
|
Pages: 1 2 3 4 5 6 7 Prev: Using 'final' with local variables Next: confused with LayoutManagement: JSplitPane |