Issue with default rounding numbers in Processing Java Mode

Dear ladies and gentlemen,

Recently, I realised I faced a problem with default settings and specifically the rounding numbers ability.
When I let Processing count up the first sum using the float function, it doesn’t show of the real answer, which is 13.05. Even using the nf() function (for strings) I still can’t round it to its real answer.
I wonder, how do I fix that problem on my computer?
Thanks in advance!

Kind regards,
The DuckHater.

The result you get is not too surprising, because the pre-processor does not know how to deal with the numbers (are they integer are they float). You need to give it a bit of help:-

float b=(10+(500/1000.0f))/60;
float a=(2+b)*6;
println(a);
println(nf(1/3.0f, 2, 10));

But even then the result is bit strange

 13.049999
00.3333333433                                                                                                                                                                                                          
1 Like

Next time please insert code with </>. You’ll get more answers when people don’t have to type your code, instead of copying it from code section.

Calculation of b. Because you use only integer values calculations are done as integers and result zero is then stored to b as float. If you change the first line to

float b=(10+(500/1000.0))/60;

It’s a common practise to use explicit float value as divider.

Second problem comes from how floating point numbers are presented internally. They are not accurate, they can be slightly off like value of a 13.049999.

1 Like