diff --git a/spec/primitives/int_spec.cr b/spec/primitives/int_spec.cr index 603dc4f51b72..e2a62a5357dd 100644 --- a/spec/primitives/int_spec.cr +++ b/spec/primitives/int_spec.cr @@ -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 diff --git a/src/compiler/crystal/interpreter/instructions.cr b/src/compiler/crystal/interpreter/instructions.cr index f2b8abcb9a51..968b9897004c 100644 --- a/src/compiler/crystal/interpreter/instructions.cr +++ b/src/compiler/crystal/interpreter/instructions.cr @@ -278,6 +278,7 @@ require "./repl" u128_to_f32: { pop_values: [value : UInt128], push: true, + overflow: true, code: value.to_f32, }, u128_to_f64: { @@ -285,6 +286,11 @@ require "./repl" 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, diff --git a/src/compiler/crystal/interpreter/primitives.cr b/src/compiler/crystal/interpreter/primitives.cr index 968361fb6c1d..29209ae59e5f 100644 --- a/src/compiler/crystal/interpreter/primitives.cr +++ b/src/compiler/crystal/interpreter/primitives.cr @@ -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)