bigdecimal

Logarithm of a BigDecimal

a 夏天 提交于 2019-11-26 12:22:56
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. Peter Java Number Cruncher: The Java Programmer's Guide to Numerical Computing provides a solution using Newton's Method . Source code from the book is available here . The following has been taken from chapter 12.5 Big Decmial Functions (p330 & p331): /** * Compute the natural

Square root of BigDecimal in Java

两盒软妹~` 提交于 2019-11-26 11:47:08
Can we compute the square root of a BigDecimal in Java by using only the Java API and not a custom-made 100-line algorithm? I've used this and it works quite well. Here's an example of how the algorithm works at a high level. Edit: I was curious to see just how accurate this was as defined below. Here is the sqrt(2) from an official source : (first 200 digits) 1.41421356237309504880168872420969807856967187537694807317667973799073247846210703885038753432764157273501384623091229702492483605585073721264412149709993583141322266592750559275579995050115278206057147 and here it is using the approach

ArithmeticException thrown during BigDecimal.divide

大城市里の小女人 提交于 2019-11-26 11:16:04
问题 I thought java.math.BigDecimal is supposed to be The Answer™ to the need of performing infinite precision arithmetic with decimal numbers. Consider the following snippet: import java.math.BigDecimal; //... final BigDecimal one = BigDecimal.ONE; final BigDecimal three = BigDecimal.valueOf(3); final BigDecimal third = one.divide(three); assert third.multiply(three).equals(one); // this should pass, right? I expect the assert to pass, but in fact the execution doesn\'t even get there: one.divide

MongoDB - What about Decimal type of value?

大兔子大兔子 提交于 2019-11-26 10:28:31
问题 I am currently learning and applying MongoDB for a small financial related project. When I read MongoDB in Action, it says: The only other issue that commonly arises with BSON numeric types is the lack of decimal support. This means that if you’re planning on storing currency values in MongoDB, you need to use an integer type and keep the values in cents. My financial related product will involve some currency values, but I am little bit confused or worried about the above statement. Here are

What is the equivalent of the Java BigDecimal class in C#?

半世苍凉 提交于 2019-11-26 08:12:49
BigDecimal is a class in the java.math package that has a lot of benefits for handling big numbers of a certain scale. Is there an equivalent class or data type in c# with this feature. C# only has BigInteger built it (in .NET framework 4). Is decimal enough precision for your task? It's a 128-bit number that can hold values in the range ±1.0 × 10 −28 to ±7.9 × 10 28 . Just recently I also needed an arbitrary precision decimal in C# and came across the idea posted here: https://stackoverflow.com/a/4524254/804614 I then completed the draft to support all basic arithmetic and comparison

BigDecimal - to use new or valueOf

被刻印的时光 ゝ 提交于 2019-11-26 08:04:13
问题 I came across two ways of getting BigDecimal object out of a double d. 1. new BigDecimal(d) 2. BigDecimal.valueOf(d) Which would be a better approach? Would valueOf create a new object? In general (not just BigDecimal), what is recommended - new or valueOf? Thanks. 回答1: Those are two separate questions: "What should I use for BigDecimal ?" and "What do I do in general?" For BigDecimal : this is a bit tricky, because they don't do the same thing . BigDecimal.valueOf(double) will use the

Java BigDecimal trigonometric methods

限于喜欢 提交于 2019-11-26 07:49:18
问题 I am developing a mathematical parser which is able to evaluate String like \'5+b*sqrt(c^2)\' . I am using ANTLR for the parsing and make good progress. Now I fell over the Java class BigDecimal and thought: hey, why not thinking about precision here. My problem is that the Java API does not provide trigonometric methods for BigDecimal s like java.lang.Math . Do you know if there are any good math libraries like Apache Commons out there that deal with this problem? The other questions is how

Java BigDecimal: Round to the nearest whole value

谁说我不能喝 提交于 2019-11-26 07:34:43
问题 I need the following results 100.12 -> 100.00 100.44 -> 100.00 100.50 -> 101.00 100.75 -> 101.00 .round() or .setScale() ? How do I go about this? 回答1: You can use setScale() to reduce the number of fractional digits to zero. Assuming value holds the value to be rounded: BigDecimal scaled = value.setScale(0, RoundingMode.HALF_UP); System.out.println(value + " -> " + scaled); Using round() is a bit more involved as it requires you to specify the number of digits to be retained. In your

Convert seconds value to hours minutes seconds?

自闭症网瘾萝莉.ら 提交于 2019-11-26 07:23:50
问题 I\'ve been trying to convert a value of seconds (in a BigDecimal variable) to a string in an editText like \"1 hour 22 minutes 33 seconds\" or something of the kind. I\'ve tried this: String sequenceCaptureTime = \"\"; BigDecimal roundThreeCalc = new BigDecimal(\"0\"); BigDecimal hours = new BigDecimal(\"0\"); BigDecimal myremainder = new BigDecimal(\"0\"); BigDecimal minutes = new BigDecimal(\"0\"); BigDecimal seconds = new BigDecimal(\"0\"); BigDecimal var3600 = new BigDecimal(\"3600\");

Is there a decimal math library for JavaScript?

社会主义新天地 提交于 2019-11-26 06:44:49
问题 Is there a mature library for doing decimal-based math, possibly arbitrary-precision, in JavaScript? Edit: I want this information for a reference page on floating-point-related problems and alternatives to use when binary floating-point is inappropriate: http://floating-point-gui.de/ 回答1: There's been a "port" of the Java BigDecimal class (I think it's here: http://freshmeat.net/projects/js_bigdecimal/ ) for a long time. I looked at it a long time ago and it seemed kind-of cumbersome and