bigdecimal

What programming languages support arbitrary precision arithmetic?

What programming languages support arbitrary precision arithmetic and could you give a short example of how to print an arbitrary number of digits? ...

Is there a difference between BigDecimal("0") and BigDecimal.ZERO?

Either for comparisons or initialization of a new variable, does it make a difference which one of these you use? I know that BigDecimal.ZERO is a 1.5 feature, so that's a concern, but assuming I'm using 1.5 does it matter? Thanks. ...

Representing Monetary Values in Java

I understand that BigDecimal is recommended best practice for representing monetary values in Java. What do you use? Is there a better library that you prefer to use instead? ...

Very, very large C# floating-point numbers

I am doing some population modeling (for fun, mostly to play with the concepts of Carrying Capacity and the Logistics Function). The model works with multiple planets (about 100,000 of them, right now). When the population reaches carrying capacity on one planet, the inhabitants start branching out to nearby planets, and so on. Problem:...

Raising a decimal to a power of decimal ?

The .net framework provides in the Math class a method for powering double. But by precision requirement I need to raise a decimal to a decimal power [ Pow(decimal a, decimal b) ]. Does the framework have such a function? Does anyone know of a library with this kind of function? ...

BigDecimal pain

How is it that Java's BigDecimal can be this painful? Double d = 13.3D; BigDecimal bd1 = new BigDecimal(d); BigDecimal bd2 = new BigDecimal(String.valueOf(d)); System.out.println("RESULT 1: "+bd1.toString()); System.out.println("RESULT 2: "+bd2.toString()); RESULT 1: 13.300000000000000710542735760100185871124267578125 RESULT 2: 13.3...

What to do with Java BigDecimal performance?

I write currency trading applications for living, so I have to work with monetary values (it's a shame that Java still doesn't have decimal float type and has nothing to support arbitrary-precision monetary calculations). "Use BigDecimal!" — you might say. I do. But now I have some code where performance is an issue, and BigDecimal is mo...

Rounding a Java BigDecimal to the nearest interval

I have a BigDecimal calculation result which I need to round to the nearest specified interval (in this case it's the financial market tick size). e.g. Price [Tick Size] -> Rounded Price 100.1 [0.25] -> 100 100.2 [0.25] -> 100.25 100.1 [0.125] -> 100.125 100.2 [0.125] -> 100.25 Thanks. Update: schnaader's solution, translated into J...

How to handle multiplication of numbers close to 1

I have a bunch of floating point numbers (Java doubles), most of which are very close to 1, and I need to multiply them together as part of a larger calculation. I need to do this a lot. The problem is that while Java doubles have no problem with a number like: 0.0000000000000000000000000000000001 (1.0E-34) they can't represent some...

Logarithm of a BigDecimal

How can I calculate the logarithm of a BigDecimal? Does anyone know of any algorithms I can use? My googling so far has come up with the (useless) idea of just converting to a double and using Math.log. I will provide the precision of the answer required. edit: any base will do. If it's easier in base x, I'll do that. ...

Javascript BigDecimal library?

Is there a good javascript BigDecimal library out there? I saw this one: http://www.navioo.com/javascript/BigDecimal_for_JavaScript_959.html# (also known as http://stz-ida.de/html/oss/js_bigdecimal.html.en) But that looks like it was autogenerated from java to javascript. It's 180K and declares global variables all over the place. I d...

Why is Ruby BigDecimal returning a weird value?

Hi, I am writing code that will deal with currencies, charges, etc.. I am going to use the BigDecimal class for math & storage. We ran into something weird with it, however. Using this statement: 1876.8 == BigDecimal('1876.8') it returns false. If I run those values through a formatting string "%.13f" I get: "%.20f" % 1876.8 => ...

BigDecimal: Can the Java compiler optimize away multiplications by 1?

Does the Java compiler remove multiplications by 1, when talking about BigDecimal? I'm standing in something similar to this: BigDecimal bd = getBigDecimal();//get arbitrary bigDecimal, could be anyone. bd = bd.multiply(new BigDecimal(getOneTwoOrThree()); Where the getOneTwoOrThree method is declared as: /* * Always returns Integ...

Java, BigDecimal. Problems with division

Hi. I'm trying to calculate a percentage "factor". That is, given a 20%, convert it into 0.2 (my intention is to later multiply values by that and get the 20% of the values). Anyway, the question is related with this piece of code: public static void main(String[] args) { int roundingMode = BigDecimal.ROUND_FLOOR; BigDecimal hundred...

Why does Java BigDecimal return 1E+1?

Why does this code sometimes return 1E+1 whilst for other inputs (e.g. 17) the output is not printed in scientific notation? BigDecimal bigDecimal = BigDecimal.valueOf(doubleValue).multiply(BigDecimal.valueOf(100d)).stripTrailingZeros(); System.out.println("value: " + bigDecimal); ...

How to getScale on BigDecimal?

I found BigDecimal have setScale method, but doesn't have getScale method. How to getScale on BigDecimal? ...

Why is the Bigdecimal(double d) construction still around?

I've noticed substantial pain over this constructor (even on this forum). People use it even though the documentation clearly states: The results of this constructor can be somewhat unpredictable http://java.sun.com/javase/6/docs/api/java/math/BigDecimal.html#BigDecimal(double) I've even seen a JSR-13 being APPROVED with a recomm...

Check if BigDecimal is integer value

Can anyone recommend an efficient way of determining whether a BigDecimal is an integer value in the mathematical sense? At present I have the following code: private boolean isIntegerValue(BigDecimal bd) { boolean ret; try { bd.toBigIntegerExact(); ret = true; } catch (ArithmeticException ex) { ret...

Why does Ruby's BigDecimal represent numbers oddly sometimes?

I am seeing very, VERY strange behavior when I run certain reports: >> p = BigDecimal.new('0.1785990254E5') => #<BigDecimal:b649b978,'0.1785990254E5',16(16)> >> q = BigDecimal.new('0.76149149E4') => #<BigDecimal:b64968d8,'0.76149149E4',8(16)> >> p-q => #<BigDecimal:b6495ab4,'0.124498764E5',16(32)> >> p.to_s => "17859.90254" >> q.to_s =>...

Java library to check whether a String contains a number *without* exceptions

I'm looking for a method that returns a boolean if the String it is passed is a valid number (e.g. "123.55e-9", "-333,556"). I don't want to just do: public boolean isANumber(String s) { try { BigDecimal a = new BigDecimal(s); return true; } catch (NumberFormatException e) { return false; } } Clea...