Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 19 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -1 +1,20 @@
# rain.math.float

Decimal floating point math implemented in Solidity/Yul.

## Context

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

🧹 Nitpick (assertive)

Consider adding follow-ups: API links and behavior specifics

Given maintainer preference to iterate docs via issues, propose opening issues to add:

  • Link(s) to LibDecimalFloat/DecimalFloat and the bit‑layout section
  • Rounding policy and overflow/underflow semantics
  • A short usage snippet (constructing/parsing, add/mul, comparisons)
  • Notes that all bytes32 Float values are valid for gt/lt/eq without revert

I can draft these issues with checklists if you want.

🧰 Tools
🪛 LanguageTool

[grammar] ~5-~5: Use correct spacing
Context: ...implemented in Solidity/Yul. ## Context IEEE 754 Floating point math such as is ...

(QB_NEW_EN_OTHER_ERROR_IDS_5)

🤖 Prompt for AI Agents
In README.md around line 5, add follow-up issue entries (since maintainers
prefer iterating via issues) linking to the LibDecimalFloat/DecimalFloat docs
and the bit-layout section, and create separate issues to document rounding
policy and overflow/underflow semantics, add a short usage snippet
(construct/parse, add/mul, comparisons), and note that all bytes32 Float values
are valid for gt/lt/eq without revert; include checklists in each issue so they
can be tracked and assign or tag me if you want me to draft the issue bodies.


IEEE 754 Floating point math such as is used by JavaScript is usually bad in finance for a few reasons.

- The decimal representations of amounts and prices of things don't have exact representations in the underlying binary
- Mathematical nonsense like dividing by 0 gives "special" values like `Infinity` and `NaN` which then propagate throughout business logic rather than erroring

This lib provides decimal floats that do error upon nonsense.

Everything you can type into a webform, or Rainlang, etc. that fits into 224 bit coefficient with 32 bit exponent (huge values for both) will be exactly represented.

This doesn't mean the floating point math is perfect, for example 1/3 will still be some imprecise rounded 0.3333... value as we don't have infinite precision.

It does mean that `0.2+0.7` is `0.9` rather than `0.8999999999999999` because the fractional values use decimal exponents rather than binary exponents.

e.g. `0.2` internally is something like `2e-1` and `0.7` is `7e-1` so internally the result is `2+7` which is `9` with an exponent of `-1`.
Comment on lines +3 to +20

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

🧹 Nitpick (assertive)

Tighten wording, fix hyphenation/grammar, and clarify behavior; propose an editorial pass

Suggested rewrite improves clarity, tone, and technical precision (hyphenation, spacing, and neutral language), and aligns with the library’s decimal exponent and bit‑layout design.

-Decimal floating point math implemented in Solidity/Yul.
+Decimal floating‑point math implemented in Solidity/Yul.

 ## Context

-IEEE 754 Floating point math such as is used by JavaScript is usually bad in finance for a few reasons.
+IEEE 754 binary floating‑point (as used by JavaScript) is a poor fit for many financial use cases:

-- The decimal representations of amounts and prices of things don't have exact representations in the underlying binary
-- Mathematical nonsense like dividing by 0 gives "special" values like `Infinity` and `NaN` which then propagate throughout business logic rather than erroring
+- Many decimal fractions cannot be represented exactly in binary floating‑point formats.
+- Operations such as division by zero produce special values (`Infinity`, `NaN`) that can propagate through computations instead of reverting.

-This lib provides decimal floats that do error upon nonsense.
+This library provides decimal floats that revert on invalid operations rather than producing `NaN`/`Infinity`.

-Everything you can type into a webform, or Rainlang, etc. that fits into 224 bit coefficient with 32 bit exponent (huge values for both) will be exactly represented.
+Any finite decimal literal whose coefficient fits within 224 bits with a 32‑bit decimal exponent range is exactly representable in this format. Internally, the `Float` type packs a 224‑bit signed coefficient and a 32‑bit signed exponent into 32 bytes.

-This doesn't mean the floating point math is perfect, for example 1/3 will still be some imprecise rounded 0.3333... value as we don't have infinite precision.
+Not all values have exact decimal expansions; for example, 1/3 will be rounded (0.3333…), as precision is finite.

-It does mean that `0.2+0.7` is `0.9` rather than `0.8999999999999999` because the fractional values use decimal exponents rather than binary exponents.
+It does mean that `0.2 + 0.7` evaluates to `0.9`, because fractional values use base‑10 exponents rather than base‑2 exponents.

-e.g. `0.2` internally is something like `2e-1` and `0.7` is `7e-1` so internally the result is `2+7` which is `9` with an exponent of `-1`.
+For example, `0.2` is represented approximately as `2e-1` and `0.7` as `7e-1`; adding them yields `9e-1` (i.e., `0.9`).
📝 Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
Decimal floating point math implemented in Solidity/Yul.
## Context
IEEE 754 Floating point math such as is used by JavaScript is usually bad in finance for a few reasons.
- The decimal representations of amounts and prices of things don't have exact representations in the underlying binary
- Mathematical nonsense like dividing by 0 gives "special" values like `Infinity` and `NaN` which then propagate throughout business logic rather than erroring
This lib provides decimal floats that do error upon nonsense.
Everything you can type into a webform, or Rainlang, etc. that fits into 224 bit coefficient with 32 bit exponent (huge values for both) will be exactly represented.
This doesn't mean the floating point math is perfect, for example 1/3 will still be some imprecise rounded 0.3333... value as we don't have infinite precision.
It does mean that `0.2+0.7` is `0.9` rather than `0.8999999999999999` because the fractional values use decimal exponents rather than binary exponents.
e.g. `0.2` internally is something like `2e-1` and `0.7` is `7e-1` so internally the result is `2+7` which is `9` with an exponent of `-1`.
Decimal floating-point math implemented in Solidity/Yul.
## Context
IEEE 754 binary floating-point (as used by JavaScript) is a poor fit for many financial use cases:
- Many decimal fractions cannot be represented exactly in binary floating-point formats.
- Operations such as division by zero produce special values (`Infinity`, `NaN`) that can propagate through computations instead of reverting.
This library provides decimal floats that revert on invalid operations rather than producing `NaN`/`Infinity`.
Any finite decimal literal whose coefficient fits within 224 bits with a 32-bit decimal exponent range is exactly representable in this format. Internally, the `Float` type packs a 224-bit signed coefficient and a 32-bit signed exponent into 32 bytes.
Not all values have exact decimal expansions; for example, 1/3 will be rounded (0.3333…), as precision is finite.
It does mean that `0.2 + 0.7` evaluates to `0.9`, because fractional values use base-10 exponents rather than base-2 exponents.
For example, `0.2` is represented approximately as `2e-1` and `0.7` as `7e-1`; adding them yields `9e-1` (i.e., `0.9`).
🧰 Tools
🪛 LanguageTool

[grammar] ~3-~3: There might be a mistake here.
Context: # rain.math.float Decimal floating point math implemented in Solidity/Yul. ...

(QB_NEW_EN_OTHER)


[grammar] ~3-~3: Use correct spacing
Context: ... point math implemented in Solidity/Yul. ## Context IEEE 754 Floating point math su...

(QB_NEW_EN_OTHER_ERROR_IDS_5)


[grammar] ~5-~5: Use correct spacing
Context: ...implemented in Solidity/Yul. ## Context IEEE 754 Floating point math such as is ...

(QB_NEW_EN_OTHER_ERROR_IDS_5)


[uncategorized] ~7-~7: If this is a compound adjective that modifies the following noun, use a hyphen.
Context: ... in Solidity/Yul. ## Context IEEE 754 Floating point math such as is used by JavaScript is u...

(EN_COMPOUND_ADJECTIVE_INTERNAL)


[grammar] ~7-~7: There might be a mistake here.
Context: ...l. ## Context IEEE 754 Floating point math such as is used by JavaScript is usuall...

(QB_NEW_EN_OTHER)


[grammar] ~7-~7: There might be a mistake here.
Context: ... Floating point math such as is used by JavaScript is usually bad in finance for a few rea...

(QB_NEW_EN_OTHER)


[grammar] ~7-~7: Use correct spacing
Context: ...sually bad in finance for a few reasons. - The decimal representations of amounts a...

(QB_NEW_EN_OTHER_ERROR_IDS_5)


[grammar] ~9-~9: There might be a mistake here.
Context: ...representations in the underlying binary - Mathematical nonsense like dividing by 0...

(QB_NEW_EN_OTHER)


[grammar] ~10-~10: There might be a mistake here.
Context: ...es "special" values like Infinity and NaN which then propagate throughout busines...

(QB_NEW_EN_OTHER)


[grammar] ~10-~10: There might be a mistake here.
Context: ...hout business logic rather than erroring This lib provides decimal floats that do...

(QB_NEW_EN_OTHER)


[grammar] ~12-~12: Use correct spacing
Context: ...imal floats that do error upon nonsense. Everything you can type into a webform, ...

(QB_NEW_EN_OTHER_ERROR_IDS_5)


[grammar] ~14-~14: Ensure spelling is correct
Context: ...nsense. Everything you can type into a webform, or Rainlang, etc. that fits into 224 b...

(QB_NEW_EN_ORTHOGRAPHY_ERROR_IDS_1)


[grammar] ~14-~14: There might be a mistake here.
Context: ...u can type into a webform, or Rainlang, etc. that fits into 224 bit coefficient with...

(QB_NEW_EN_OTHER)


[grammar] ~14-~14: Use proper capitalization
Context: ...bform, or Rainlang, etc. that fits into 224 bit coefficient with 32 bit exponent (hu...

(QB_NEW_EN_OTHER_ERROR_IDS_6)


[grammar] ~14-~14: Use proper capitalization
Context: ...that fits into 224 bit coefficient with 32 bit exponent (huge values for both) will...

(QB_NEW_EN_OTHER_ERROR_IDS_6)


[grammar] ~14-~14: Use correct spacing
Context: ...s for both) will be exactly represented. This doesn't mean the floating point mat...

(QB_NEW_EN_OTHER_ERROR_IDS_5)


[grammar] ~16-~16: There might be a mistake here.
Context: ...esented. This doesn't mean the floating point math is perfect, for example 1/3 w...

(QB_NEW_EN_OTHER)


[grammar] ~16-~16: There might be a mistake here.
Context: ...doesn't mean the floating point math is perfect, for example 1/3 will still be some impr...

(QB_NEW_EN_OTHER)


[grammar] ~16-~16: There might be a mistake here.
Context: ...the floating point math is perfect, for example 1/3 will still be some imprecise rounde...

(QB_NEW_EN_OTHER)


[grammar] ~16-~16: There might be a mistake here.
Context: ...ill be some imprecise rounded 0.3333... value as we don't have infinite precision. I...

(QB_NEW_EN_OTHER)


[grammar] ~16-~16: Use correct spacing
Context: ...lue as we don't have infinite precision. It does mean that 0.2+0.7 is 0.9 rat...

(QB_NEW_EN_OTHER_ERROR_IDS_5)


[grammar] ~18-~18: Use correct spacing
Context: ... exponents rather than binary exponents. e.g. 0.2 internally is something like ...

(QB_NEW_EN_OTHER_ERROR_IDS_5)


[grammar] ~20-~20: There might be a mistake here.
Context: ...onents rather than binary exponents. e.g. 0.2 internally is something like `2e-...

(QB_NEW_EN_OTHER)


[grammar] ~20-~20: There might be a mistake here.
Context: ...e.g. 0.2 internally is something like 2e-1 and 0.7 is 7e-1 so internally the r...

(QB_NEW_EN_OTHER)


[grammar] ~20-~20: There might be a mistake here.
Context: ...y is something like 2e-1 and 0.7 is 7e-1 so internally the result is 2+7 which...

(QB_NEW_EN_OTHER)


[grammar] ~20-~20: There might be a mistake here.
Context: ...is7e-1so internally the result is2+7which is9with an exponent of-1`. ...

(QB_NEW_EN_OTHER)


[grammar] ~20-~20: There might be a mistake here.
Context: ...which is9with an exponent of-1`.

(QB_NEW_EN)

🤖 Prompt for AI Agents
In README.md around lines 3 to 20, the copy uses informal phrasing, inconsistent
hyphenation, and ambiguous technical wording (e.g., "Decimal floating point
math", "does error upon nonsense"); perform an editorial pass to (1) use
consistent terminology like "decimal floating-point arithmetic", (2) replace
colloquialisms with precise phrases such as "raises an error on invalid
operations" instead of "does error upon nonsense", (3) normalize hyphenation
(e.g., "floating-point", "bit-layout"), capitalization and punctuation, (4)
clarify the representation guarantee as "exact representation for values whose
decimal coefficient fits in 224 bits with a 32-bit exponent" and keep the 1/3
explanation concise about rounding, and (5) tidy examples and code fragments
(e.g., `0.2 + 0.7` → `0.9`) for consistency and readability.

Comment on lines +18 to +20

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

🧹 Nitpick (assertive)

Great illustrative example—keep it, just minor spacing/notation polish

The 0.2 + 0.7 example is valuable and correctly motivates decimal exponents; the suggested edit above only adds spacing and a clearer “For example” sentence.

🧰 Tools
🪛 LanguageTool

[grammar] ~18-~18: Use correct spacing
Context: ... exponents rather than binary exponents. e.g. 0.2 internally is something like ...

(QB_NEW_EN_OTHER_ERROR_IDS_5)


[grammar] ~20-~20: There might be a mistake here.
Context: ...onents rather than binary exponents. e.g. 0.2 internally is something like `2e-...

(QB_NEW_EN_OTHER)


[grammar] ~20-~20: There might be a mistake here.
Context: ...e.g. 0.2 internally is something like 2e-1 and 0.7 is 7e-1 so internally the r...

(QB_NEW_EN_OTHER)


[grammar] ~20-~20: There might be a mistake here.
Context: ...y is something like 2e-1 and 0.7 is 7e-1 so internally the result is 2+7 which...

(QB_NEW_EN_OTHER)


[grammar] ~20-~20: There might be a mistake here.
Context: ...is7e-1so internally the result is2+7which is9with an exponent of-1`. ...

(QB_NEW_EN_OTHER)


[grammar] ~20-~20: There might be a mistake here.
Context: ...which is9with an exponent of-1`.

(QB_NEW_EN)

🤖 Prompt for AI Agents
In README.md around lines 18 to 20, polish the example sentence by replacing
"e.g." with "For example," and adjust spacing/notation for clarity: ensure
spaces around operators and commas, use consistent inline code formatting for
numeric literals and expressions (e.g., `0.2`, `0.7`, `0.9`, `2e-1`, `7e-1`),
and rephrase the second sentence to read clearly as "For example, `0.2`
internally is `2e-1` and `0.7` is `7e-1`, so the result is `2+7` with an
exponent of `-1`, yielding `0.9`."

Loading