Skip to content

Fix Float32#abs for signed zeros#15814

Merged
straight-shoota merged 2 commits intocrystal-lang:masterfrom
HertzDevil:bug/float32-abs
May 26, 2025
Merged

Fix Float32#abs for signed zeros#15814
straight-shoota merged 2 commits intocrystal-lang:masterfrom
HertzDevil:bug/float32-abs

Conversation

@HertzDevil
Copy link
Contributor

Like #12424, but for Float32.

@HertzDevil HertzDevil added kind:bug A bug in the code. Does not apply to documentation, specs, etc. topic:stdlib:numeric labels May 22, 2025
@straight-shoota straight-shoota added this to the 1.17.0 milestone May 22, 2025
@straight-shoota straight-shoota merged commit ab30482 into crystal-lang:master May 26, 2025
51 of 54 checks passed
@HertzDevil HertzDevil deleted the bug/float32-abs branch May 26, 2025 09:27
straight-shoota added a commit that referenced this pull request Feb 12, 2026
This fixes a regression from #15814 which unintentionally changed the return type of `Float32#abs` to `Float64`. Previously, it returned `Float32`.

`Math.copysign(self, 1)` falls back to the overload without restrictions which converts both arguments to `Float64` and thus returns `Float64`.

This patch fixes that by explicitly targeting the `Math.copysign(Float32, Float32)` overload for `Float32#abs`.
`Float64#abs` is not broken, but still applies an unnecessary integer-to-float conversion when we should be using a float right away.

I discovered this type mismatch while researching #13389
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

kind:bug A bug in the code. Does not apply to documentation, specs, etc. topic:stdlib:numeric

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants