cc65-floatlib icon indicating copy to clipboard operation
cc65-floatlib copied to clipboard

Division problem

Open jcal710 opened this issue 3 years ago • 5 comments

I am trying to get a decimal number into a float. So I did this

Y=fdiv(itof((int)14278),itof((int)10000));

Which should result in 1.4278. Instead I get 1.42779994.

I know there is some rounding error when dealing with floats but I didn't think it was this bad. Am I doing something wrong?

jcal710 avatar Jul 03 '22 20:07 jcal710

Did you use a recent checkout, ie after i changed the binary format to IEEE754? That might be more accurate, before that it used just two bytes for the mantissa.

That said, there may well be other problems with the rounding and int/float conversion - i did not do any extensive tests.

mrdudz avatar Jul 03 '22 20:07 mrdudz

Looks like I grabbed it 3 days before the last update. I'll pull and try again.

jcal710 avatar Jul 03 '22 21:07 jcal710

If you are adventurous, you could also try https://github.com/mrdudz/cc65/tree/fptest (see https://github.com/cc65/cc65/pull/1777 )

mrdudz avatar Jul 03 '22 21:07 mrdudz

I love a good adventure. The latest update did not change the result, so I'll give fptest a try.

jcal710 avatar Jul 03 '22 21:07 jcal710

The problem is likely the crude way i am converting from int to float, basically just truncating the mantissa instead of rounding it.

mrdudz avatar Jul 03 '22 21:07 mrdudz