String to Float, without introducing errors

Greg Ewing greg.ewing at canterbury.ac.nz
Sun Dec 18 16:35:39 EST 2022


On 19/12/22 6:35 am, Paul St George wrote:
> So I am working on a physics paper with a colleague. We have a theory about Newtons Cradle.
> 
> We want to illustrate the paper with animations.
> 
> Because there is a problem, I am investigating in all areas. ... I would like to be in control of or fully aware of what goes on under the bonnet.

When you convert a string to a float, you're already getting the closest
possible value in binary floating point.

For things like physics simulations, you need to design your algorithms
so that they're tolerant of small inaccuracies in the representation of
your numbers. If those are causing you problems, it sounds like there
is some kind of numerical instability in your algorithm that needs to
be addressed.

It's also possible that there is just a bug somewhere in your code, and
the problem really has nothing to do with floating point inaccuracies.

If you can post some code we might be able to help you further.

-- 
Greg


More information about the Python-list mailing list