More on Big Numbers in C++

I’ve been rather lackadaisical about fixing my “big number” classes, but I’ve finally gotten up off the couch, and I have new versions available.

I’ve changed the names of the bigint and bigdec classes to integer and decimal, respectively, because I thought the “big…” names smelled of Java.  I also wrote a small Web page that ties the three classes together.

In a comment to a previous post, Andrew Dalke suggested some additional values I could test; and, sure enough, I found a bug (thanks).  (The bug was actually in what’s now called the integer class:  I hadn’t guarded against aliasing of the operands to expressions like x *= x.)

I think I remember someone suggesting that some users might prefer classes with “more features”.  What additional features did you have in mind?  Don’t suggest trig. functions and the like:  I’ve limited the <cmath>-like functions that take decimal and rational arguments to those that return exact values.  (You might be able to talk me into square root, but that would be successive approximation using Newton’s method which is what I know how to write.  I already have a version of the rational class that has quiet NaNs and infinities and a spaceship operator, but I’m not sure I like it.)

As I’ve said before, these classes are not intended for serious numerical work; and numerics experts probably already know where to find better implementations, or could write such themselves.

Update, 2024-02-05:  I woke up this morning having in my mind a way to make rational comparisons a bit quicker, so I made that change.  I also noticed that I had failed to remove an isinf() test of the accuracy argument to the conversion from floating point values.  (In the previous version, passing a NaN for the accuracy would trigger an exact conversion using std::frexp().  In the current version, any non-finite accuracy will do that.)

By the way, if anybody out there has access to a C++ implementation where FLT_RADIX != 2, I’d appreciate a test of the frexp() business.  I have access only to boxes where FLT_RADIX is 2.

U.S. Sanctions on West-Bank Israelis

16:00 UTC−6:  DW News leads with a report that Biden has signed an executive order imposing economic sanctions on four named Israelis who were involved in anti-Palestinian violence in the West Bank.  I see this as a baby step in the right direction.  We’ll see whether U.S. policy toward Israel develops any morality.  Stay tuned…

16:30−6:  BBC World News America leads with the same story.

17:10:  my local TV news has a couple of sentences about the sanctions.  They say it’s in response to the killing of a Palestinian-American teenager.

17:30:  no mention of the sanctions on NBC Nightly News.

18:00:  nothing about the sanctions during the headlines on PBS Newshour either.

Now I’m saddened again.  NBC did have a short report about a growing movement within the Arab-American community for sitting out the next election.  They won’t vote for Trump, but they don’t think they can vote for Biden either.  Let’s hope that that doesn’t give the election to Trump.  Maybe there’s some good news in John Morales’ comment below.

More on the rational Class

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…

The Latest Cancer News

As I’ve said before, I signed up for a study of the effectiveness of prophylactic radiation to keep the cancer out of the brain.

I got a call this morning saying that I’d been randomized into the control group, so I won’t be getting the radiation; but I’ll do all the rest of it:  CT and MRI scans, some blood tests, and some tests of my cognitive abilities (I hope more interesting than the test that Trump bragged about passing, but we’ll see).  The various tests will happen (IIRC) every three months for two years.

With any luck, I’ll be able to do all the tests at an outpatient facility that’s just a ten minute drive from where I live; but it’s unclear whether some special requirements of the study mean that I have to get the scans at the hospital, a 35 minute drive at the best of times.  I hope to find out more about that tomorrow morning.

I’ve had the first scans already, and I’ll get the lab work done and the first cognitive tests tomorrow morning at the hospital (but I don’t have to show up until 10:30, so that’s after the rush hour).

Update, 2024-01-07:  the test of my cognitive abilities that I took on the 4th was indeed more interesting than the one that Trump bragged about passing, but still nothing to brag about. 😎

More on Trump and Colorado

I seem to be getting all my news on this from Mike the Mad Biologist.

A comment reports that the Supreme Court has denied certiorari (they won’t be hearing the case), which means that the Colorado decision stands; but a comment on my previous post suggests that the Colorado ruling includes a stay that’s permanent if the case is appealed.

It’s about half an hour until my Monday-Friday TV news routine begins.  We’ll see whether DW News, BBC World News America, my local news, NBC Nightly News or PBS Newshour confirms the certiorari denial and whether that means that the Colorado decision is permanently stayed or is in effect since the appeal failed.

In any event, the ruling affects the primary election, not the general election; and I read somewhere else (I can’t remember where) that Colorado Republicans are already talking about having a caucus instead of an election so there won’t be a ballot for Trump to be kept off of.

I’m making a trip to the grocery store tomorrow.  I’ll definitely be buying some popcorn.

05:30 UTC−6:  oops, it turns out that I totally misunderstood.  The question that SCOTUS declined to weigh in on was whether POTUS has immunity from prosecution on the insurrection charge, so an appeals court will decide that first.  This was reported as a minor victory for Trump because that other case could go on for quite a while.

We don’t know anything more about Colorado after all.  (I’m in the mood for some popcorn anyway.)

Trump Off the Ballot in Colorado (for now)

I just heard that the Colorado Supreme Court has ruled that Trump will not be allowed on the ballot because of the 14th Ammendment.

Now on to the U.S. Supreme Court.  There’s an outside chance that the Colorado decision might be upheld, but I’m not at all confident about that.  In any event, it’ll be fun to see what sort of hoops the Republican justices jump through to overturn Colorado.

Update 2023-12-20:  Mike the Mad Biologist has some thoughts on this.

I’m Still Here

Not much has been happening with me lately.

I had MRI and CT scans on Tuesday and both turned out OK, so on Wednesday I signed up for the study of whether prophylactic radiation is actually effective in keeping small cell cancer out of the brain.  The study is randomized but not blind, and I’ll find out on the 3rd or 4th whether I’ll be getting the radiation.

I’ve been a bit lackadaisical about finishing my C++ rational number library.  I think it’s ready to go, but I still have a bit more testing to do.  If anybody would like to suggest a change in the design, please do.

A Database Access Library

I’ve been yakking about how I’m working on a database access library, so maybe I should show you my current design to prove it. 😎  This should be the last of the dorky programming posts for a while.

The C++ standard library has nothing like the java.sql.stuff.  There are some third-party libraries out there, but they seem kind of klunky to me, and I suspect that they don’t scale up to real world use cases.  This makes C++ a really bad choice for use in the good old “business data processing” domain.*

I wanted a design that more closely matches common C++ idioms (container/iterator for example) so that coders wouldn’t have to learn a whole new way of thinking.  It’s by no means an exact match (cursors are not really iterators, for example); but I think it’s the same general idea.

I’m not ready to share any code yet.  Indeed, some of it might embarrass me. 😎  For a variety of reasons, it’s been a couple of years since I’ve worked on it; and I need to review some ODBC ugliness that I’ve forgotten to finish the SQL/CLI implementation.  I haven’t even started on the Web client business which needs to be part of the proof of concept since I claim that I can make it work.

If there’s anybody out there who thinks this is basically a good design and would like to run with it themselves, I’ll happily share what I’ve got so far (probably about 1500 lines of code, not really a big deal yet).  I’m currently stuck on conversions between C++ and SQL types, which is central to making cursors work and depends on some ODBC stuff that I can’t remember.

My goal of getting WG21 to publish this in a TS will probably never be realized, but I think it’s worth doing anyway.  I’ll implement the rational number that I mentioned in the previous post first just to have some fun and get back in the groove; then I’ll see whether I can restart the work on the database library.**


*Business data processing will probably never be more than a niche market for C++, but that’s where the bulk of my experience is and so it’s what I know most about.

**My local PBS affiliate is starting up its quarterly pledge fortnight, so I shouldn’t have much TV to watch for a couple of weeks. 😎

Rational Math

Back when I was a C++ newbie, I decided to write a rational number class because I thought it would be a good exercise.  I revised it and added features over the years as I learned new stuff (I/O manipulators, for example).  I looked around my computer for it this morning and found a version that I wrote over a decade ago when I still didn’t have even a C++11 compiler to work with.

About 11 or 12 years ago, WG21’s numerics study group was thinking about publishing a Technical Specification (TS, a kind of warning about possible future standardization) that would propose some new number types (fixed-point types, an unbounded integer, things like that).  I confidently raised my hand and said that I had a rational number that I could add and proposed what would eventually morph into this*.  That “Numbers TS” is no longer in the works, so my rational number died.

I think I’ll revisit that to bring it at least to the C++20 level and implement it using the bigint class that I mentioned in the last post to avoid overflow.  I also want to put the I/O manipulators back just because I think they’re Really Cool. 😎

That should keep my brain working for a little while longer.


*Starting with the “[rational.math] Rational math” section, that’s what much of the C++ standard reads like.  It’s most definitely not a tutorial. 😎