Scarminio
Scarminio

Reputation: 169

Float object with two different values

I'm having trouble printing a Float value into a report. When I inspect the object, it shows two values. I would expect when I print this value into a report, the value would be 80.914055. But it prints 80.789055.

Float showing two values

The value is coming from a dll writen in Fortran. There is a method that retrieves the value from the dll and return as a Java float (80.914055).

public float getN1(){
    return ttoutn.getCRUICM(0);
};

There is another class that calls the method above. But uses method.invoke() instead.

Object returnedObject = method.invoke(calculationModule);

With method.invoke(), the primitive float is wrapped in an object Float. It seems that the value inside Float is correct (80.914055), as shown above. But once you use System.out.println(), it prints as another value (80.789055).

I am kind of clueless of what might be happening.

Upvotes: 1

Views: 243

Answers (1)

lscoughlin
lscoughlin

Reputation: 2416

Because java uses IEE754 for floating point math. Which means you're going to get weird stuff sometimes.

If the precision is really important, use BigDecimal. There's great guide to this stuff here.

Upvotes: 2

Related Questions