bigdecimal

Calculating PI with BigDecimal

∥☆過路亽.° 提交于 2021-02-10 20:20:44
问题 This is my first time using a BigDecimal and I am having some troubles with it. Here is my code: import java.math.BigDecimal; public class Test { public static void main(String[] args) { BigDecimal pi = new BigDecimal(4); boolean plus = false; for (int i = 3; i < 1000000; i += 2) { if (plus) { pi.add(new BigDecimal(4).divide(new BigDecimal(i), 2, BigDecimal.ROUND_HALF_UP)); } else { pi.subtract(new BigDecimal(4).divide(new BigDecimal(i), 2, BigDecimal.ROUND_HALF_UP)); } plus = !plus; } System

BigDecimal/double Precision - number rounds up higher

不想你离开。 提交于 2021-02-10 06:19:36
问题 The second of below method calls, to setYCoordinate(), gets incorrect value -89.99999435599995 instead of -89.99999435599994. The first call to setXCoordinate() gets correct value 29.99993874900002. setXCoordinate(BigDecimal.valueOf(29.99993874900002)) setYCoordinate(BigDecimal.valueOf(-89.99999435599994)) I put a breakpoint inside BigDecimal.valueOf() - this method's code looks as below - public static BigDecimal valueOf(double val) { // Reminder: a zero double returns '0.0', so we cannot

Set MathContext to BinaryOperator reference methods

霸气de小男生 提交于 2021-02-08 04:27:10
问题 I have this enum: public enum Operator { add("+", BigDecimal::add), subtract("-", BigDecimal::subtract), multiply("*", BigDecimal::multiply), divide("/", BigDecimal::divide), mod("%", BigDecimal::remainder); Operator(final String symbol, final BinaryOperator<BigDecimal> operation) { this.symbol = symbol; this.operation = operation; } public BinaryOperator<BigDecimal> getOperation() { return operation; } } I want to use the some MathContext , one can easily do that when performing an operation

scala.math.BigDecimal : 1.2 and 1.20 are equal

左心房为你撑大大i 提交于 2021-02-07 17:32:06
问题 How to keep precision and the trailing zero while converting a Double or a String to scala.math.BigDecimal ? Use Case - In a JSON message, an attribute is of type String and has a value of "1.20". But while reading this attribute in Scala and converting it to a BigDecimal, I am loosing the precision and it is converted to 1.2 回答1: @Saurabh What a nice question! It is crucial that you shared the use case! I think my answer lets to solve it in a most safe and efficient way... In a short form it

scala.math.BigDecimal : 1.2 and 1.20 are equal

冷暖自知 提交于 2021-02-07 17:31:16
问题 How to keep precision and the trailing zero while converting a Double or a String to scala.math.BigDecimal ? Use Case - In a JSON message, an attribute is of type String and has a value of "1.20". But while reading this attribute in Scala and converting it to a BigDecimal, I am loosing the precision and it is converted to 1.2 回答1: @Saurabh What a nice question! It is crucial that you shared the use case! I think my answer lets to solve it in a most safe and efficient way... In a short form it

scala.math.BigDecimal : 1.2 and 1.20 are equal

三世轮回 提交于 2021-02-07 17:30:39
问题 How to keep precision and the trailing zero while converting a Double or a String to scala.math.BigDecimal ? Use Case - In a JSON message, an attribute is of type String and has a value of "1.20". But while reading this attribute in Scala and converting it to a BigDecimal, I am loosing the precision and it is converted to 1.2 回答1: @Saurabh What a nice question! It is crucial that you shared the use case! I think my answer lets to solve it in a most safe and efficient way... In a short form it

Set all BigDecimal operations to a certain precision?

回眸只為那壹抹淺笑 提交于 2021-02-07 05:20:20
问题 My Java program is centered around high precision calculations, which need to be accurate to at least 120 decimal places. Consequentially, all non-integer numbers will be represented by BigDecimals in the program. Obviously I need to specify the accuracy of the rounding for the BigDecimals, to avoid infinite decimal expressions etc. Currently, I find it a massive nuisance to have to specify the accuracy at every instantiation or mathematical operation of a BigDecimal. Is there a way to set a

Set all BigDecimal operations to a certain precision?

别说谁变了你拦得住时间么 提交于 2021-02-07 05:16:46
问题 My Java program is centered around high precision calculations, which need to be accurate to at least 120 decimal places. Consequentially, all non-integer numbers will be represented by BigDecimals in the program. Obviously I need to specify the accuracy of the rounding for the BigDecimals, to avoid infinite decimal expressions etc. Currently, I find it a massive nuisance to have to specify the accuracy at every instantiation or mathematical operation of a BigDecimal. Is there a way to set a

Stange values after BigDecimal initialization

微笑、不失礼 提交于 2021-01-28 19:15:30
问题 I'm trying to initialize a HashMap in the foolowing way: HashMap<BigDecimal,BigDecimal> myMap = new HashMap<>(); myMap .put(new BigDecimal(1.7), new BigDecimal(1.5)); myMap .put(new BigDecimal(3.3), new BigDecimal(3)); myMap .put(new BigDecimal(5), new BigDecimal(4.5)); myMap .put(new BigDecimal(6.6), new BigDecimal(6)); myMap .put(new BigDecimal(11), new BigDecimal(10)); myMap .put(new BigDecimal(16.5), new BigDecimal(15)); but tha value above insert are change in the following wang: 1.7

How can tensorflow do basic math with integer data larger than 32 bits?

橙三吉。 提交于 2021-01-28 05:11:54
问题 What's the best way to do simple math using int64, BigInt or even float64 values with tensors? I mention that I want to do simple math to say that I don't think that backing the tensor with a String will do the trick (though I'm open to anything at this point). By simple math, I mean add, subtract, multiply, divide and compare. I'm a tensorflow newbie, using version 1.1.2 of tensorflow for javascript in Chrome. Here's an example of Math.PI yielding an incorrect tensor value: Math.PI; // 3