Mailing Lists: Apple Mailing Lists

Image of Mac OS face in stamp
 
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Math conundrum



On Dec 19, 2004, at 3:04 PM, email@hidden wrote:

I now have another question. In looking at this there seem to be some fundamental floating point problems that arise between base 10 number entry and base 2 computer representation. Namely that 1.8, (say as an example number, I might not be remembering right that this was an actual example number given,) might be a reasonably nice and exactly precise number in base 10 but it does not have a nice and exactly precise conversion to base 2 which results in you ending up with an inexact and imprecise base 2 representation.

Sure, but likewise there are obvious a.b values in base 2 which have no equivalent in base 10.


OK, I am currently looking at some 'c' to java exact, to the best of my abilities, ports of some log code. So, I have a double I now want to return as probably a BigDecimal. Do I return more precise values by going double -> Double -> String -> BigDecimal or is the precision possibly already doomed and I might just as well just go double -> BigDecimal?

if it's already a double, then it's already base-2 "imprecise", and susceptible as such. Converting it to a String buys you nothing, though you might do new BigDecimal("1.8");


An easy but inefficient way to get a numerical (non-string) value of the form a.b into BigDecimal precisely is to say: new BigDecimal(ab).movePointLeft(1)
For example, new BigDecimal(18).movePointLeft(1) is 1.8 exactly.


Sean

_______________________________________________
Do not post admin requests to the list. They will be ignored.
Java-dev mailing list      (email@hidden)
Help/Unsubscribe/Update your Subscription:
http://lists.apple.com/mailman/options/java-dev/email@hidden

This email sent to email@hidden


Visit the Apple Store online or at retail locations.
1-800-MY-APPLE

Contact Apple | Terms of Use | Privacy Policy

Copyright © 2007 Apple Inc. All rights reserved.