Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there a way to set the Floating-Point Unit's rounding mode in Java?

I want to know how to round a floating-point number to a machine floating number(for example double). The number "0.01111116" cannot be represented by machine floating point, in some rounding mode, this number should be represented as "0.011111159999999995" with some precision loss. But I don't know how to finish this in Java? So I want to know the API to set the rounding mode to get the exact representation of machine floating-point number in Java. Thanks!

like image 236
gingeredder Avatar asked Oct 25 '25 06:10

gingeredder


1 Answers

The Java specification does not provide any means to control the floating-point rounding mode. Round-to-nearest is used.

It is not generally possible to arrange for floating-point arithmetic to produce mathematically exact results, so software must be designed to tolerate and adjust for errors or, in very special cases, to get exact results by using extra care.

like image 135
Eric Postpischil Avatar answered Oct 26 '25 19:10

Eric Postpischil