Freedom-of-Form-Foundation / anatomy3d

A CAD tool for humanoid anatomy alterations. See the anatomy3d-blender repository for more recent work.
https://freedomofform.org/1856/3d-anatomy-project-scope-phase-1-focus-on-a-limb-joint/
GNU General Public License v2.0
7 stars 5 forks source link

Create Real type #18

Closed AdamNorberg closed 3 years ago

AdamNorberg commented 3 years ago

Real is a type that, in debug mode, acts like a float, and in release mode, acts like a double. A compiler flag can be provided to override this behavior.

I didn't reimplement the Math library, so you'll have to do some casts to and from double if you want to use it. If that gets annoying, implement convenience methods at your discretion.

It builds, but it hasn't been tested yet. My next major project is to get a unit test engine up and going and start writing unit tests in this codebase, probably starting right here.

Extra features compared to double: operator ! is implemented, converting zero (and negative zero) to 1.0, nonzero non-NaN values to 0.0, and NaN remains NaN, which seems like the most appropriate interpretation of unary "!" in floating-point. Many things that are static methods on Double are instance properties on Real. I've added IsNear, IsAbsolutelyNear, and IsRelativelyNear to implement "close enough equality". (With all the warnings about "aaaagh! never compare floating-point values for exact equality! aaaaarrrgh!" that languages and compilers give us, you'd think the language libraries would build this in. Well, I'm writing this one, so I did.

Please review.

AdamNorberg commented 3 years ago

The merge, of course, should be a squash commit, nobody really cares about the 40 commits I made along the way. Pretty much every time I save the file, I commit to my working branch with notes on what I did so I can roll back if/when I screw up.

AdamNorberg commented 3 years ago

Done. Added copy constructor, comment on GetHashCode, explicit casts away from float (the cast to double is optional but the cast to decimal is indeed required), and a single missing period. Thanks for the review! Anything else?

AdamNorberg commented 3 years ago

Whoops! I messed up my true and false operator implementations - I got them backwards, and I failed to add the NaN check I needed. Fixed.

Lathreas commented 3 years ago

Oh dear, well spotted! I completely overlooked that as well, thinking you were going for the 0=true model I've seen some embedded projects use.

I'll do another quick review of the changes you made.

AdamNorberg commented 3 years ago

I've added the integer-typed constructors, cast-in operators (implicit), and cast-out operators (explicit).

The variable names in the cast-out operators changed to "real" for the very simple reason that this is what JetBrains Rider's "guess a new variable name here" template expander wanted and just going with that is much less effort than going back and shorting it to "r" when I'm writing this much boilerplate.

AdamNorberg commented 3 years ago

@the-og-gear or @Lathreas , please merge unless you have more comments.