Hacker News new | past | comments | ask | show | jobs | submit login

Students can be taught using the standard library printf() or whatever.

It's not as easy as "just round" though, because you don't know where the digits start to repeat. A smart function would also print 4.1249999999999 as 4.125.

A fancy unicode version could even include the mathematical notation to print 3.333333333333 as 3.3 with a line over the trailing 3. Or 4.2525252525 as 4.25 with the bar over the 25. Might be asking too much of a simple print function though, this might end up being an extension of the halting problem.




Sounds like you want a computer algebra system, not a floating point format algorithm.


I don't think I'm the only one. I was thinking as I wrote the response that such a system might have to include a whole lot more math than it looks at the surface, which is probably why it doesn't exist as a general solution.

But maybe there could be some heuristics that get it right 99% of the time (with a don't use this library for serious number crunching disclaimer).


I've used various HP scientific and graphing calculators that have functions to convert a floating point number to a symbolic representation of a rational number, or a fraction of pi. These functions are approximate, and their tolerance for how large the denominator of the fraction may get is determined by the current setting for how many decimal digits the display of numbers is rounded to. In practice, this works pretty well, and they implemented this capability long before they had a full CAS on any of their calculators.


Maybe. I'm having difficulty thinking of a case where you would want to know that a number was 1/2 pi (as opposed to 1.57...), but you wouldn't want to know about 3/19 pi.


It sounds like what you want to do is print the number with the smallest denominator (from the range of numbers represented by this bit pattern). A quick search turns up a plausible formula for this: https://math.stackexchange.com/questions/2494774/questions-c...




Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: