Skip to content

Commit

Permalink
Fix Float#abs and Number#format for -0.0 (#12424)
Browse files Browse the repository at this point in the history
straight-shoota authored Aug 31, 2022
1 parent 4d5832c commit 34fe2c2
Showing 4 changed files with 49 additions and 1 deletion.
13 changes: 13 additions & 0 deletions spec/std/float_spec.cr
Original file line number Diff line number Diff line change
@@ -422,4 +422,17 @@ describe "Float" do
(nan <=> 1_i32).should be_nil
(nan <=> 1_i64).should be_nil
end

it "#abs" do
Math.copysign(1, 0.0.abs).should eq 1
Math.copysign(1, -0.0.abs).should eq 1

0.1.abs.should eq 0.1
-0.1.abs.should eq 0.1

Float64::MAX.abs.should eq Float64::MAX
Float64::MIN.abs.should eq -Float64::MIN
Float64::INFINITY.abs.should eq Float64::INFINITY
(-Float64::INFINITY).abs.should eq Float64::INFINITY
end
end
31 changes: 31 additions & 0 deletions spec/std/humanize_spec.cr
Original file line number Diff line number Diff line change
@@ -28,6 +28,8 @@ describe Number do
it { assert_prints 0.0.format(decimal_places: 1), "0.0" }
it { assert_prints 0.0.format(decimal_places: 1, only_significant: true), "0.0" }

it { assert_prints 0.01.format(decimal_places: 1), "0.0" }

it { assert_prints 123.45.format, "123.45" }
it { assert_prints 123.45.format(separator: ','), "123,45" }
it { assert_prints 123.45.format(decimal_places: 3), "123.450" }
@@ -44,6 +46,35 @@ describe Number do
it { assert_prints 1e-5.format(decimal_places: 7), "0.0000100" }
it { assert_prints 1e-4.format(decimal_places: 7), "0.0001000" }

it { assert_prints -1.format, "-1" }
it { assert_prints -12.format, "-12" }
it { assert_prints -123.format, "-123" }
it { assert_prints -1234.format, "-1,234" }

it { assert_prints -1.format(decimal_places: 1), "-1.0" }
it { assert_prints -1.format(decimal_places: 1, only_significant: true), "-1.0" }

it { assert_prints -0.0.format(decimal_places: 1), "-0.0" }
it { assert_prints -0.0.format(decimal_places: 1, only_significant: true), "-0.0" }

it { assert_prints -0.01.format(decimal_places: 1), "-0.0" }

it { assert_prints -123.45.format, "-123.45" }
it { assert_prints -123.45.format(separator: ','), "-123,45" }
it { assert_prints -123.45.format(decimal_places: 3), "-123.450" }
it { assert_prints -123.45.format(decimal_places: 3, only_significant: true), "-123.45" }
it { assert_prints -123.4567.format(decimal_places: 3), "-123.457" }

it { assert_prints -123_456.format, "-123,456" }
it { assert_prints -123_456.format(delimiter: '.'), "-123.456" }

it { assert_prints -123_456.789.format, "-123,456.789" }

it { assert_prints -1e15.format(decimal_places: 7), "-1,000,000,000,000,000.0000000" }
it { assert_prints -1e15.to_i64.format(decimal_places: 7), "-1,000,000,000,000,000.0000000" }
it { assert_prints -1e-5.format(decimal_places: 7), "-0.0000100" }
it { assert_prints -1e-4.format(decimal_places: 7), "-0.0001000" }

it { assert_prints Float64::MAX.format, "179,769,313,486,231,570,814,527,423,731,704,356,798,070,567,525,844,996,598,917,476,803,157,260,780,028,538,760,589,558,632,766,878,171,540,458,953,514,382,464,234,321,326,889,464,182,768,467,546,703,537,516,986,049,910,576,551,282,076,245,490,090,389,328,944,075,868,508,455,133,942,304,583,236,903,222,948,165,808,559,332,123,348,274,797,826,204,144,723,168,738,177,180,919,299,881,250,404,026,184,124,858,368.0" }
it { assert_prints Float64::MIN.format, "-179,769,313,486,231,570,814,527,423,731,704,356,798,070,567,525,844,996,598,917,476,803,157,260,780,028,538,760,589,558,632,766,878,171,540,458,953,514,382,464,234,321,326,889,464,182,768,467,546,703,537,516,986,049,910,576,551,282,076,245,490,090,389,328,944,075,868,508,455,133,942,304,583,236,903,222,948,165,808,559,332,123,348,274,797,826,204,144,723,168,738,177,180,919,299,881,250,404,026,184,124,858,368.0" }
it { assert_prints Float64::MAX.format(decimal_places: 0), "179,769,313,486,231,570,814,527,423,731,704,356,798,070,567,525,844,996,598,917,476,803,157,260,780,028,538,760,589,558,632,766,878,171,540,458,953,514,382,464,234,321,326,889,464,182,768,467,546,703,537,516,986,049,910,576,551,282,076,245,490,090,389,328,944,075,868,508,455,133,942,304,583,236,903,222,948,165,808,559,332,123,348,274,797,826,204,144,723,168,738,177,180,919,299,881,250,404,026,184,124,858,368" }
4 changes: 4 additions & 0 deletions src/float.cr
Original file line number Diff line number Diff line change
@@ -275,6 +275,10 @@ struct Float64
Number.expand_div [Int8, UInt8, Int16, UInt16, Int32, UInt32, Int64, UInt64, Int128, UInt128], Float64
Number.expand_div [Float32], Float64

def abs
Math.copysign(self, 1)
end

def ceil : Float64
LibM.ceil_f64(self)
end
2 changes: 1 addition & 1 deletion src/humanize.cr
Original file line number Diff line number Diff line change
@@ -56,7 +56,7 @@ struct Number
int_size = integer.size
dec_size = decimals.size

io << '-' if self < 0
io << '-' if number.is_a?(Float::Primitive) ? Math.copysign(1, number) < 0 : number < 0

start = int_size % group
start += group if start == 0

0 comments on commit 34fe2c2

Please sign in to comment.