I am trying to convert an arbitrary string (whereby the decimal point is explicitly manipulated controlled) to a number (i.e. floating point) using string-to-number
and I am running into either a rounding issue or some kind of precision issue when Emacs performs the conversion. I am not a floating point expert so my exact terminology may be slightly incorrect, please pardon me.
The problem is interment based on the value (leading me to believe it's one of the aforementioned issues) provided, however I have an example reproducible case:
Example code:
(string-to-number "8807240867.266221")
;; Expected: 8807240867.266221
;; Actual: 8807240867.26622
Standard environment disclosure:
- OS: MacOS
- Chip: Intel
- Emacs Version: 27.2.1
- Port: Mitsuharu Yamamoto / Railwaycat
- Other Notes: - No special distributions / etc.
- Relevant Common Packages Used:
- Standard internal Emacs packages
The trailing 1
is removed when I would not expect it to be. I have searched the C-code for how string-to-number
works, however I am quite limited in my C knowledge so my best guess is that somehow the value is overflowing somewhere down the line? However, I don't receive an error message of any kind so that is what makes me think it's some kind of rounding/precision problem instead.
Any help is appreciated in explaining why this kind of issue is occurring, what exact issue is occurring, how to prevent it/properly fix it so that no digits are unexpectedly chopped off.