Skip to content
Merged
Show file tree
Hide file tree
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
7 changes: 7 additions & 0 deletions spec/primitives/int_spec.cr
Original file line number Diff line number Diff line change
Expand Up @@ -137,4 +137,11 @@ describe "Primitives: Int" do
expect_raises(OverflowError) { Float32::MAX.to_u128.succ.to_f32 } # Float32::MAX == 2 ** 128 - 2 ** 104
end
end

describe "#to_f!" do
it "doesn't raise on overflow for UInt128#to_f32" do
UInt128::MAX.to_f32!.should eq(Float32::INFINITY)
Float32::MAX.to_u128.succ.to_f32!.should eq(Float32::MAX)
end
end
end
6 changes: 6 additions & 0 deletions src/compiler/crystal/interpreter/instructions.cr
Original file line number Diff line number Diff line change
Expand Up @@ -278,13 +278,19 @@ require "./repl"
u128_to_f32: {
pop_values: [value : UInt128],
push: true,
overflow: true,
code: value.to_f32,
},
u128_to_f64: {
pop_values: [value : UInt128],
push: true,
code: value.to_f64,
},
u128_to_f32_bang: {
pop_values: [value : UInt128],
push: true,
code: value.to_f32!,
},
f32_to_f64: {
pop_values: [value : Float32],
push: true,
Expand Down
2 changes: 1 addition & 1 deletion src/compiler/crystal/interpreter/primitives.cr
Original file line number Diff line number Diff line change
Expand Up @@ -815,7 +815,7 @@ class Crystal::Repl::Compiler
in {.u128?, .u32?} then checked ? u128_to_u32(node: node) : pop(8, node: node)
in {.u128?, .u64?} then checked ? u128_to_u64(node: node) : pop(8, node: node)
in {.u128?, .u128?} then nop
in {.u128?, .f32?} then u128_to_f32(node: node)
in {.u128?, .f32?} then checked ? u128_to_f32(node: node) : u128_to_f32_bang(node: node)
in {.u128?, .f64?} then u128_to_f64(node: node)
in {.f32?, .i8?} then f32_to_f64(node: node); checked ? f64_to_i8(node: node) : f64_to_i64_bang(node: node)
in {.f32?, .i16?} then f32_to_f64(node: node); checked ? f64_to_i16(node: node) : f64_to_i64_bang(node: node)
Expand Down