-
Notifications
You must be signed in to change notification settings - Fork 22
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
C output: Change from Doubles to Floats #7
Comments
I agree that Floats should be enough, and comparing to 32-bit Vensim makes sense. |
It also turns out that Vensim stores all values as floats, even when it does double-precision computations. https://www.vensim.com/documentation/index.html?ref_variable_names.htm |
I did a quick experiment to change all uses of doubles to floats (actually an There are some good performance and size wins to be had from this change (see below), but we'd need to spend some time making sure that any differences in the new outputs are within an acceptable range, so I think we should hold off on the change until we get some more automated checks in place. The runtime performance gains appear to be very slight on modern processors; the main benefit appears to be in shaving off ~120kb from the wasm binary size. Here are some performance numbers for the record relative to other performance work. PerformanceMacBook Pro (2019) | 2.4 GHz 8-core i9, 32 GB RAM, macOS 10.15
iPhone 8 | A11, iOS 13
iPad Air (2013) | A7, iOS 12
Size
Legend
|
We use 64-bit math in SDEverywhere because of a long-standing rule in C that all internal math operations are 64 bit, even if the operands are floats, and results are ultimately rounded to 32 bit precision. I elected to maintain 64-bit precision throughout by storing data in C doubles.
But your comment led me to look into this further yesterday. I discovered that in the C99 standard that modern compilers like clang use, the precision of internal math operations is implementation dependent. And guess what? Clang does not enforce 64 bit precision internally. If you use float operands, math operations are 32-bit.
I think moving to 32-bit math makes sense for SDEverywhere. That is enough precision for SD work. Most people use the 32 bit version of Vensim. We could remove the requirement for validating against data from 64-bit Vensim. And we would cut our memory requirements for data in half. This mostly involves changing "double" to "float" everywhere and using the float versions of standard library functions.
-Todd
The text was updated successfully, but these errors were encountered: