Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to perform high precision calculations with mutable class in Java?

Tags:

java

I'm designing a tool using Java 6, that will read data from Medical Devices.

Each Medical Device manufacturer implements its own Firmware/Protocol. Vendors (like me) write their own interface that uses the manufacturer's firmware commands to acquire data from the Medical Device. Most firmwares will output data in a cryptic fashion, so the vendor receiving it, is supposed to scale it by doing some calculations on it, in order to figureout the true value.

Its safe to assume that medical data precision is as important as financial data precision etc.

I've come to the conclusion of using BigDecimal to do all numerical calculations and store the final value. I'll be receiving a new set of data almost every second, which means, I'll be doing calculations and updating the same set of values every second. Example:Data coming across from a ventilator for each breath.

Since BigDecimal is immutable, I'm worried about the number of objects generated in the heap every second. Especially since the tool will have to scale up to read data from lets say 50 devices at the same time.

I can increase the heap size and all that, but still here's my questions....

Questions

  1. Is there any mutable cousin of BigDecimal I could use?

  2. Is there any existing opensource framework to do something like this?

  3. Is Java the right language for this kind of functionality?

  4. Should I look into Apfloat? But Apfloat is immutable too. How about JScience?

  5. Any Math library for Java I can use for high precision

  6. I'm aiming for a precision of upto 10 digits only. Dont need more than that. So whats the best library or course of action for this type of precision?

like image 931
FatherFigure Avatar asked Apr 19 '11 19:04

FatherFigure


People also ask

Is Java BigDecimal immutable?

Since BigDecimal is immutable, these operations do not modify the existing objects. Rather, they return new objects.

How do you do BigDecimal arithmetic operations in Java?

Java BigDecimal basic arithmetic operations In the example, we have addition, subtraction, multiplication, and division operations. var val1 = new BigDecimal("3.44"); var val2 = new BigDecimal("2.74"); We create two BigDecimal values. The numbers are passed as strings.

What is the difference between double and BigDecimal in Java?

A BigDecimal is an exact way of representing numbers. A Double has a certain precision. Working with doubles of various magnitudes (say d1=1000.0 and d2=0.001 ) could result in the 0.001 being dropped altogether when summing as the difference in magnitude is so large. With BigDecimal this would not happen.

How do you scale BigDecimal?

scale() is an inbuilt method in java that returns the scale of this BigDecimal. For zero or positive value, the scale is the number of digits to the right of the decimal point. For negative value, the unscaled value of the number is multiplied by ten to the power of the negation of the scale.


1 Answers

I would recommend that before you jump to the conclusion that BigDecimal doesn't suit your needs, that you actually profile your scenario. It is not a foregone conclusion that immutable nature is going to have a significant impact on your scenario. A modern JVM is very good at allocate and destroying large quantities of objects.

like image 85
Konstantin Komissarchik Avatar answered Sep 28 '22 06:09

Konstantin Komissarchik