Skip to content
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

Decimal Type determined at Compile Time (#26) #126

Merged
merged 30 commits into from
Dec 2, 2023
Merged

Conversation

mookums
Copy link
Member

@mookums mookums commented Nov 24, 2023

For Issue #26.

Allow you to build Lost using either float or double by providing a decimal type. The decimal.hpp also provides a STR_TO_DECIMAL macro that expands to stof and stod for floats and doubles respectively.

Injects the flags (defined in databases.hpp on database serialization.
Runtime DB checking on de-serialization to ensure that the decimal was the same across both.

It will use the float type by default but this can be changed to double by using make LOST_DATABASE_DOUBLE=1.

Tested both float and double functionality using the img_7760.png example and they both worked.

@mookums
Copy link
Member Author

mookums commented Nov 24, 2023

Passes the random_crap.sh and readme_examples_test.sh in both float and double mode if those matter.

@markasoftware
Copy link
Member

Oh whoops somehow I missed the email for this. I'll take a look!

Copy link
Member

@markasoftware markasoftware left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good overall, left a few comments!

And one more thing which will probably be pretty annoying, but necessary: Try compiling with -Wdouble-promotion (eg, CXXFLAGS=-Wdouble-promotion make) to find any instances where double is still being used when compiling in float mode. There are a few :) I think ultimately we'll need to wrap every decimal constant with ((decimal)180.0) for example.

Makefile Outdated Show resolved Hide resolved
src/decimal.hpp Outdated Show resolved Hide resolved
src/databases.cpp Outdated Show resolved Hide resolved
src/main.cpp Outdated Show resolved Hide resolved
src/io.cpp Show resolved Hide resolved
Makefile Outdated Show resolved Hide resolved
@mookums
Copy link
Member Author

mookums commented Nov 28, 2023

Yeah, we might just have to. Got rid of a lot of the promotions by defining new decimal based macros for all the math stuff like M_PI.

Would it be better for us to inline/define wrappers for pow and other math methods so we only need to type cast the literals?

Having the decimal typecast everywhere doesn't feel right.

@mookums
Copy link
Member Author

mookums commented Nov 28, 2023

Pushed a lot of changes regarding enforcing the usage of decimal in a variety of places. This also includes the prevention of double promotion. I set up another run for the tests so the suite can run on the LOST_FLOAT_MODE version.

Ending up opting for a macro strategy in terms of enforcing decimal. You can find all of the new macros plus some more notes about them in decimal.hpp. This system is closer to macro hell but it's a lot more readable than having a bunch of (decimal) typecasts all over the place.

The test suite keeps failing when running in float mode due to a weird issue (seems like a float inaccuracy issue?) regarding the basic consistency check loop. There's an example here if you want to take a look at it. I can probably fix it if I get a little guidance on the purpose of that test and what we should be expecting.

Thanks!

@markasoftware
Copy link
Member

markasoftware commented Nov 28, 2023 via email

Copy link
Member

@markasoftware markasoftware left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's nearly done!

Makefile Outdated Show resolved Hide resolved
src/attitude-estimators.cpp Outdated Show resolved Hide resolved
src/decimal.hpp Outdated
#define DECIMAL_M_SQRT1_2 (decimal) M_SQRT1_2 /* 1/sqrt(2) */

// Math Functions wrapped with Decimal typecast
#define DECIMAL_POW(base,power) (decimal) std::pow(base, power)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do we actually need DECIMAL versions of all these functions? My understanding is that the C++ versions (eg std::pow instead of C pow from math.h) are properly overloaded to return the same type as the arguments

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Seems like it. I tried both math.h and cmath and they both cause a double promotion to occur. The other solution is to wrap every pow or abs call with the DECIMAL macro but it gets pretty unwieldy with all of the parenthesis. This solution is macro heavy but it works well for our intended use case while preserving readability.

src/io.cpp Show resolved Hide resolved
src/databases.cpp Outdated Show resolved Hide resolved
@markasoftware
Copy link
Member

About the failing test -- it's basically taking a database, running a query on it, and then checking that the results of the query all lie within the range that was requested. However, looking at it now, I'm not actually sure it's correct, because FindPairsLiberal is allowed to return stuff slightly out of range -- could you try changing the FindPairsLiberal to FindPairsExact and see if the errors go away?

@mookums
Copy link
Member Author

mookums commented Nov 29, 2023

Changed it to utilize the FindPairsExact and now it works for both float and double mode as expected.

src/decimal.hpp Outdated
// because the code becomes hard to read when there are multiple layers of typecasting.
// With this method, we might have more preprocessing to do BUT the code remains readable
// as the methods remain relatively the same.
#define DECIMAL(x) (decimal) x
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Final comment, let's make sure all these macros are "safe" (or maybe convert them to overloaded or templated functions), for example DECIMAL(x)++ (though a very weird statement) would cause x to be incremented before being converted to decimal due to order of operations (so at minimum let's put a layer of parenthesis around the body of DECIMAL)

@markasoftware
Copy link
Member

lgtm let's merge it

@mookums mookums merged commit fc664ef into master Dec 2, 2023
2 checks passed
@mookums mookums deleted the muki-decimal-#26 branch December 2, 2023 20:48
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants