Double changes numbers apperently?

So I am currently making a mandelbrot programm and I noticed it kind of doesn’t zoom where I want. After a long search I found the source of the problem:

When giving it the place to zoom in it chnges the number when converting it to a double.

Here is a small programm to show the problem:

double test=(double)(-1.7448119236955923);

When using it it should show you a different version in the print field
here is my output

-1.7448118925094604 0.0

The difference between the numbers is 0,0000000311861319.

Why is this and how can I prevent it?

The difference between




is 0…

That is, you’ve subtracted a number from the same number. The result is zero.

EDIT: Oh! You want to know why the NON-ZERO number is different! Right.

This is due to the way that floats (or doubles) are represented in memory. They get rounded, because you can’t express every single possible float number there is with the number of bits a float uses to store the value.

1 Like

Is there a way to prevent it?

Ok I made some progress. I can use

double test=new Double("-1.7448119236955923");

and it doesn’t have the effect I described.

You discovered floating point rounding errors. Worse yet is when you try to test for equality with ==. You just need to be somewhat careful and understand the limitations of digital computers.