My decimal is too small

Here is my example of my code:


value = -1 / (value1 - value2);
println(xdirection);

It is very simple. I am trying to take the negative reciprocal of a value. But the number is too small, and when I try to print the vale, it shows up as 0.0. I tried to use double, but even with double, the value is still 0.0. Is there a way where I can calculate small numbers?

Even if I do this:

println(-1/100);

The value still shows up as zero. How can I fix this? Thank you

Try

println(-1.0/100.0f);

what does the f do? Does it stand for float?

1 Like

Yes

but .0 alone should be enough

Without the.0: When java assumes there are two ints, it turns the result into int as well

@Chrisir === i have got some big problems these days working with this kind of numbers (that i have solved using BigDecimals) and perhaps you can explain what happens: let us take some very simple example:


void setup(){
  float f = -1.0/100000.0f;
  println(f);
  float fbis = f*10000f;
  println(fbis);
  
}
  • As you can see at some moment scientific notation is used: when exactly???
  • As you can see the 2° returned value is not exact…
  • As in my app i work with numbers (lat, long) which are very precise this kind of error is not possible; i have (for experience!) casted the same values to double; result is not the same but it is false…for the first value!
1 Like

I don’t know about these things

Maybe even println does some roundings internally

There are an infinite number of floating point numbers and since the computer has finite memory, not all floating point numbers can be stored exactly, its impossible.

Computers generally use the IEEE754 standard for storing float and double precision numbers.

In your example code the first line performs the division and stores the result as a float. The actual bit pattern used to store this value is in fact represents the number -0.00000099999994 which will be displayed as -1.0E-5 so when multiplied to give fbis the results is -0.099999994

2 Likes

@Chrisir ===
i dont think that println()is the culprit: in the first case it rounds (nor floor, nor ceil) nothing it returns what is expected… anyway thanks to answer! - As i have told i found the concrete solution using BigDecimals, but i like to understand… And i learn…Knowing that i have simplified (too much?) because you can (that s mys case) get the values as string (in Scientific notation) and have to parse them but how?- Float parseFloat() in this type of case returns weird values, the same for Double.parseDouble()… So what?

2 Likes

@quark ===
thanks! - What you say makes sense and so i can see that “full precision” is at max 16 decimals digits.

1 Like