diff --git a/compiler/compiler.go b/compiler/compiler.go index 1e25ef32..76e7aac9 100644 --- a/compiler/compiler.go +++ b/compiler/compiler.go @@ -3306,19 +3306,19 @@ func (c *Compiler) parseConvert(typeFrom, typeTo types.Type, value llvm.Value) ( if typeFrom.Info()&types.IsFloat != 0 && typeTo.Info()&types.IsInteger != 0 { // Conversion from float to int. - if typeTo.Info()&types.IsUnsigned != 0 { // to signed int - return c.builder.CreateFPToSI(value, llvmTypeTo, ""), nil - } else { // to unsigned int + if typeTo.Info()&types.IsUnsigned != 0 { // if unsigned return c.builder.CreateFPToUI(value, llvmTypeTo, ""), nil + } else { // if signed + return c.builder.CreateFPToSI(value, llvmTypeTo, ""), nil } } if typeFrom.Info()&types.IsInteger != 0 && typeTo.Info()&types.IsFloat != 0 { // Conversion from int to float. - if typeFrom.Info()&types.IsUnsigned != 0 { // from signed int - return c.builder.CreateSIToFP(value, llvmTypeTo, ""), nil - } else { // from unsigned int + if typeFrom.Info()&types.IsUnsigned != 0 { // if unsigned return c.builder.CreateUIToFP(value, llvmTypeTo, ""), nil + } else { // if signed + return c.builder.CreateSIToFP(value, llvmTypeTo, ""), nil } } diff --git a/testdata/float.go b/testdata/float.go index dd11b516..6e2d5bf7 100644 --- a/testdata/float.go +++ b/testdata/float.go @@ -24,6 +24,19 @@ func main() { println(float32(f64)) println(float64(f32)) + // float -> int + var f1 float32 = 3.3 + var f2 float32 = 5.7 + var f3 float32 = -2.3 + var f4 float32 = -11.8 + println(int32(f1), int32(f2), int32(f3), int32(f4)) + + // int -> float + var i1 int32 = 53 + var i2 int32 = -8 + var i3 uint32 = 20 + println(float32(i1), float32(i2), float32(i3)) + // complex64 c64 := complex(f32, 1.2) println(c64) diff --git a/testdata/float.txt b/testdata/float.txt index a5d688bf..e20f0e52 100644 --- a/testdata/float.txt +++ b/testdata/float.txt @@ -11,6 +11,8 @@ +3.333333e-001 +6.666667e-001 +6.666667e-001 +3 5 -2 -11 ++5.300000e+001 -8.000000e+000 +2.000000e+001 (+6.666667e-001+1.200000e+000i) +6.666667e-001 +1.200000e+000