WeekendMonkey
Posts: 2
Joined: Mon Feb 03, 2020 12:12 pm

Math Calculations Are Slightly Off

Mon Feb 03, 2020 12:40 pm

Hi,

I have a QT C++ application that computes sunrise and sunset times using functions from math.h and double datatypes. When I build the project for my Linux desktop the results are spot-on (compared against online resources). I have just cross compiled the same project to run on my Raspberry Pi 3B, but the results are all slightly wrong, even though the system clock times are identical on both machines.

One other oddity is that QT Creator's code editor complains about not being able to find math.h when I select my Raspberry Pi Kit, but the project builds and links without issue.

My gut feeling is that floats are being used instead of doubles, but work gets in the way of debugging :D I am new to QT and cross compiling for the Pi, so I would appreciate any suggestions to help me locate the cause, or understand any restrictions.

Thanks,
Steve

User avatar
jojopi
Posts: 3191
Joined: Tue Oct 11, 2011 8:38 pm

Re: Math Calculations Are Slightly Off

Mon Feb 03, 2020 2:41 pm

WeekendMonkey wrote:
Mon Feb 03, 2020 12:40 pm
My gut feeling is that floats are being used instead of doubles
Not a terrible guess (the avr-gcc compiler used for Arduino does implement single-precision for performance reasons). However, all models of Raspberry Pi have double-precision floating-point in hardware.

You mention cross compilation, but if the project already runs on Linux, then why not compile it on the Pi itself using the standard Raspbian tools?

If you cannot include math.h, then the compiler may assume that functions return int instead of double, but I would expect that to give drastically wrong results, not "slightly off".

We cannot diagnose or debug your code unless you post it, or—even better—a minimal example program that produces different results between your two systems.

(Personally, I would prefer the actual code. Astronomical calculations are an interest of mine, and I would like to see your handling of the polar circles.)

njh
Posts: 69
Joined: Wed Aug 09, 2017 2:07 pm

Re: Math Calculations Are Slightly Off

Mon Feb 03, 2020 7:21 pm

How wrong are the results? If the differences are tiny, it might not be wrong at all. Many math.h functions are not covered by the IEEE standard and won't always give the same answer on different platforms (though the Pi's accuracy should not be uniformly worse than a x86 PC). There are also compiler flags that might be enabled (such as GCC's -ffast-math) that allow it to rearrange FP expressions in ways that get a slightly different answer from the standard, again different on different platforms or toolchains.

Though if things are substantially wrong the above won't explain it.

WeekendMonkey
Posts: 2
Joined: Mon Feb 03, 2020 12:12 pm

Re: Math Calculations Are Slightly Off

Mon Feb 03, 2020 9:10 pm

Thanks for the replies. I solved the issue by switching to QtMath and using the qreal data type, now both versions are in synch. and correct.

To answer your questions, if the answers are still of interest:

Previously the Pi version was wrong by over an hour for the rise and set times.

I am developing on a Linux laptop because my Pi is in a case with a 7" display. The laptop is just more convenient and means I can test code away from the Pi.

I can't lay any claim to the algorithms in use, I have converted the java code provided at http://conga.oan.es/~alonso/doku.php?id ... n_position. This provides more accuracy than I really need (a weather station) and fortunately I don't need to worry about the polar circles.

Return to “C/C++”