Why we don’t use Doubles for Financial Calculations

[color=red]原文地址:[url]http://bloodredsun.com/?p=320[/url]
另一篇文章:What Every Computer Scientist Should Know About Floating-Point Arithmetic[url]http://download.oracle.com/docs/cd/E19957-01/806-3568/ncg_goldberg.html[/url][/color]

My current client is in the middle of hiring some Java developers and as I
mentioned earlier in March (Interviewing – the importance of PASSION!) I’ve been doing some of the interviewing. One of the things we’ve done is to create a technical task to see how the candidates actually code. It’s a simple exercise that requires them to think through some of the basics of financial operations and one thing that has surprised me has been the common use of doubles to represent financial values. It’s been highlighted for some time that this is not a great thing to do but someone actually challenged me to show that it wasn’t.

So here we go…

view plaincopy to clipboardprint?
package com.bloodredsun;

public class DoubleOperation {

public static void main(String[] args) {

double t1 = 10.266d;
double t2 = 10.0d;

//Outputs 0.266
System.out.println(t1-t2);

double h1 = 100.266d;
double h2 = 100.0d;

//Outputs 0.26600000000000534
System.out.println(h1-h2);
}
}
Ouch! That is not what we want but it is the classic behaviour of doubles. The inability to represent some decimals in the IEEE-754 format (as binary fractions) causes this. If we want correct precision the answer is to use BigDecimals but we have to remember to use Strings in the constructors or you end up with the same issues that you were trying to avoid.

view plaincopy to clipboardprint?
package com.bloodredsun;

import java.math.BigDecimal;

public class BigDecimalOperation {

public static void main(String[] args) {

BigDecimal t1 = new BigDecimal("10.266");
BigDecimal t2 = new BigDecimal("10.0");

//Outputs 0.266
System.out.println(t1.subtract(t2));

BigDecimal h1 = new BigDecimal("100.266");
BigDecimal h2 = new BigDecimal("100.0");

//Outputs 0.266
System.out.println(h1.subtract(h2));
}
}
That’s great but wouldn’t it be nice to use the normal operators rather than the overly-verbose method calls for the mathematical operations.

Now there is no way that we can do this in Java but if we let ourselves use another language on the JVM…

view plaincopy to clipboardprint?
package com.bloodredsun

object ScalaBigDecimalOperation {

def main (args: Array[String]) {
var t1 = BigDecimal("10.266")
var t2 = BigDecimal("10.0")
//Outputs 0.266
println(t1 - t2)

var h1 = BigDecimal("100.266")
var h2 = BigDecimal("100.0")
//Outputs 0.266
println(h1 - h2)
}
}
Scala FTW!

PS if you want to know more about floating point operations have a read of What Every Computer Scientist Should Know About Floating-Point Arithmetic
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值