Andrew Dalke did a few tests of my rational number library (thanks) and I thought it deserved a post of its own.
The first links to code in the function float_as_integer_ratio_impl(), which is the C implementation of the Python method as_integer_ratio() …
OK, I found it. Yeah, I tried to use frexp(); but I tried to do some bit twidling to get the mastissa bits as an integer and that was a big mistake. It’s easy once you know how to do it. 😎
I’ll add something like this to my rational class; but I’m not sure yet whether I want to make it only for FLT_RADIX == 2 and whether I want to allow the user to do something less than an exact conversion, falling back on the continued fractions routine if I need to.
I wrote a quick and dirty program to do the tests mentioned in the comment and compiled it:
– for Windows using an ancient Microsoft compiler which doesn’t conform even to C++11 (although it has rvalue references and type traits templates which is enough for it to get through my bignum stuff), and with a long double that’s just a double.
– for Linux using a somewhat more up-to-date GCC (C++14 at least), and with a bigger long double.
I got two different sets of results (Windows, Linux), possibly due to differences in the long double format.
– The Windows version had no trouble with either 0.0869406496067503 or 1.1100695288645402e-29, but it failed on 0.9999999999999999.
– The Linux version failed on both 0.0869406496067503 and 1.1100695288645402e-29, but it worked on 0.9999999999999999.
– Both failed on nextafter(1,0).
… 1.1100695288645402e-29 gives a “Can’t convert NaNs or infinities to bigint” because the
val0 = 1.0 / (val0 – static_cast(int0)); step results in an inf.
(That should be static_cast<long double>(int0)…either WordPress or the browser itself thought that the <long double> was an unrecognized HTML tag.)
My Linux version reports that as well. That’s a bug in my code which I’ll have to figure out if I decide to keep the continued fractions around for support of either FLT_RADIX != 2 or inexact conversions.
I urge you to look towards an existing library if all you need is SQL interoperability.
Actually, what I need is to keep my mind active in my retirement. 😎
There are some third-party database access libraries out there; but they look to me like they were written by teenagers sitting in their basements going, “Cool!”; and I’m not sure that they scale up to real-world applications. For example, there’s one called SOCI that actually tries to make database tables act like iostreams.
Also, I think I can make my library act like a Web service client as well, and maybe even access the cloud. We’ll see…