Sunday, February 25, 2007

Using floating point variables to represent money => not a good idea!

I was reading through some code the other day and was surprised to find that it was using doubles to represent dollar amounts. Reason for the alarm bells is that doubles and floats cannot accurately represent many decimal fractions (eg, 0.1), since doubles and floats internally work with powers of 2 rather than powers of 10. These inaccuracies are likely to lead to significant errors, especially when performing arithmetic (eg, adding up a table of dollar amounts). See this IBM article for a more in depth explanation and examples. The solution is to use types that work with powers of ten internally. In C#, you can use 'decimal' and in Java or Ruby, 'BigDecimal', to avoid these problems.

11 comments:

Anonymous said...

Using C#: You wouldn't happen to know a nice way to truncate down from 7 decimal to 4 decimal places for example.

All the Decimal members (in .NET 2.0) add a rounding aspect to any return values or only consider the integer component or round up.

We see the options as:-
1. A small amount of arithmetic to 'construct' the correct output. (E.g. maybe rounding and then 'if' either subtract or add a delta.)

2. Casting to a string and then manipulating and then casting sub strings back.

Neither of the above fell elegant though.

P.S. Great blog, I'll be back to have a bit more of read especially the book reviews.

Cheers
Ryan

James Crisp said...

Hi Ryan,

You could multiply the number by 10 to the power of the number of decimal places you are interested in, truncate the result and then divide it by the number of decimal places you are interested in. Eg, (x * 10000).truncate() / 10000.

Sorry I missed your comment earlier - Blogger has stopped notifying me of new comments. I've now moved my blog to jamescrisp.org and WordPress.

Cheers,
James

cialis soft tabs online purchase said...

vardenafil hydrochloride prescription medication
viagra soft tabs
vardenafil without prescription
where can i purchase viagra
buy cialis online uk
potenciale
how to shop for tadalafil online
vivanza
lowest price levitra generic online
superviga
order viagra soft
apcalis

Anonymous said...

viagra super active ohne rezept kaufen
zithromax online kaufen
viagra ohne rezept bestellen
cialis super active online kaufen
vermox rezeptfrei
tamiflu bestellen
sinequan online kaufen
levitra rezeptfrei kaufen
lovegra online apotheke
zithromax kaufen
zyloprim online kaufen
cialis kaufen
viagra super active kaufen

Anonymous said...

lovegra farmacia
viagra super active apotheke rezeptfrei
zyloprim ohne rezept bestellen
vendita viagra super active
pharmacie achat
farmacia senza ricetta
propecia rezeptfrei bestellen
acheter pharmacie
kamagra oral jelly france
cialis rezeptfrei
kamagra apotheke rezeptfrei
cialis soft bestellen
achat kamagra soft pharmacie
actoplus met rezeptfrei kaufen

Anonymous said...

Comprar kamagra oral jelly Farmacia
Vendita lamisil Senza Ricetta
vermox Rezeptfrei Kaufen
Acheter levitra France
Acheter du kamagra en Pharmacie
Acheter lasix France
nolvadex Rezeptfrei
Prix accutane France
Achat kamagra oral jelly en ligne
Achat accutane en ligne
Vendita effexor xr Senza Ricetta
Precio cialis super active plus Espana
Comprare kamagra oral jelly Italia
accutane Rezeptfrei

essay service said...

thanks for a good tips!

Larah said...

For college essay such presentation would be perfect but integration this prodact in production would be difficult.

online bookmakers said...

I do agree with all of the ideas you’ve presented in your post. They are really convincing and will definitely work. bet365 | online bookmakers | Betting Bonuses

Drupal Development said...

Awesome stuff. This is exactly what I have been looking for. Thanks a ton for sharing this. Social Network Design

Anonymous said...

I was reading through Cheap RS Goldby technique of some signal another evening and was astonished to obtain that it experienced been making use of doubles to symbolize dollar amounts. motive for the alarm bells is the fact that doubles and floats cannot accurately symbolize many decimal fractions (eg, 0.1), contemplating about doubles and floats internally Buy WOW Goldprovide the outcomes with powers of two quite than powers of 10.