Sometimes the simplest problems are harder than they first seem. A client once specified a requirement to me: we'll give you the VAT-inclusive (20%) price; you need to generate invoices showing the net and VAT amounts. Sounds easy, right? net = round(tot*5/6) vat = round(tot/6)
-
Show this thread
-
Some time passed. The client came back to me: "You've been calculating the invoices wrong! Our accountant has had to work an extra few days because about one in five of your invoices has an error of £0.01, and our accounts don't balance!"
1 reply 0 retweets 3 likesShow this thread -
Did you spot my error? So, what's the moral of this story? I'm not sure, because it's quite subtle, but I could claim that it's a case of "make impossible states unrepresentable". The challenge was in recognising that we were representing impossible states. Suggestions welcome.
10 replies 0 retweets 3 likesShow this thread -
Replying to @propensive
I do not get it... Have you used `double` for monetary calculation? But that would not be subtle...
2 replies 0 retweets 1 like -
Replying to @jarek000000 @propensive
What's your suggestion to deal with moneteray calculation in Scala? Do you have a library you'd recommend?
1 reply 0 retweets 0 likes -
Replying to @francoeurdavid @jarek000000
It wouldn't fix the problem I described, but an opaque Int type (or value class) with fixed precision, either set to 2 for that type, or encoded in some bits on the left, would he a good way to describe monetary values and avoid floating-point issues.
2 replies 0 retweets 0 likes -
Replying to @propensive @francoeurdavid
http://javax.money .MonetaryAmount or plain old BigDecimal
2 replies 0 retweets 1 like
I didn't even know that existed!
Loading seems to be taking a while.
Twitter may be over capacity or experiencing a momentary hiccup. Try again or visit Twitter Status for more information.