svenvc / ston

STON - Smalltalk Object Notation - A lightweight text-based, human-readable data interchange format for class-based object-oriented languages like Smalltalk.
MIT License
135 stars 32 forks source link

Fix in floating point numbers materialization #35

Closed webwarrior-ws closed 2 years ago

webwarrior-ws commented 2 years ago

Addresses issue #34 Also adds problematic values to STONWriteReadTest>>#testFloats. I'm not sure the fix got 100% of cases covered, but it's considerable improvement of the current state of things.

svenvc commented 2 years ago

Hi,

Thanks a lot for your feedback and contribution. You are right: your changes do improve accuracy while parsing floats.

I was always under the impression that one has to be careful with float equality testing, but it seems it is possible. I am also not 100% sure it is true in all cases, but let's assume it is, we will see.

Anyway, I made the following commit, which does include your other issues as well: https://github.com/svenvc/ston/commit/a9959114c3036aad1dd5c327a5b8ac3309e4c3e5

Sven

webwarrior-ws commented 2 years ago

Good.

The thing with floats is that you may get slightly different results from computations that are mathematically equivalent. That's why it's generally not recommended to test floats for equality.

But otherwise there's nothing magical about floating point numbers, which are defined by IEEE 754 standard.

Any binary float can be encoded in decimal format and decoded back without loss of data. That's because fractions in form of 2^-N can be represented as finite decimal fractions. The reverse is generally not true, but it doesn't matter for our purposes.