java - Multiply long or BigInteger by double without loss of precision -


i want multiply high precision integer (long or biginteger) small double (think > 0 , < 1) , arithmetically rounded integer (long or biginteger) of exact arithmetic value of operation result.

converting double integer not work, because fractional value gets lost.

converting integer double, multiply , converting result integer not work either, because double not precise enough. of course argue, because double operand not precise enough in first place, might not matter result not precise same order of magnitude, in case, does.

bonus question:

using bigdecimal works, seems inefficient. converting long double , multiplying , converting seems run 500 times faster (albeit losing precision) converting bigdecimal. there more efficient possibility? possible gain performance when multiplying several different long each same double?

you want use bigdecimal in order preserve precision.

biginteger mybi = new biginteger("99999999999999999"); double d = 0.123; bigdecimal bd = new bigdecimal(mybi); bigdecimal result = bd.multiply(bigdecimal.valueof(d)); 

Comments

Popular posts from this blog

jquery - How can I dynamically add a browser tab? -

node.js - Getting the socket id,user id pair of a logged in user(s) -

keyboard - C++ GetAsyncKeyState alternative -