Home  >  Article  >  Backend Development  >  Go Float32bit() result is not expected

Go Float32bit() result is not expected

WBOY
WBOYforward
2024-02-06 09:25:111129browse

Go Float32bit() 结果不是预期的

Question content

For example: 16777219.0 decimal converted to bits

16777219 -> 1 0000 0000 0000 0000 0000 0011

mantissa: 23 bit
0000 0000 0000 0000 0000 001

exponent:
24+127 = 151
151 -> 10010111

result shoud be:
0_10010111_000 0000 0000 0000 0000 0001

1001011100000000000000000000001

but:

fmt.printf("%b\n", math.float32bits(float32(16777219.0)))

// 1001011100000000000000000000010

Why is the result of go float32bit() not expected?

refer to:

  • base-conversion.ro

renew:

fmt.Printf("16777216.0:%b\n", math.Float32bits(float32(16777216.0)))
fmt.Printf("16777217.0:%b\n", math.Float32bits(float32(16777217.0)))
//16777216.0:1001011100000000000000000000000
//16777217.0:1001011100000000000000000000000
fmt.Printf("16777218.0:%b\n", math.Float32bits(float32(16777218.0)))
//16777218.0:1001011100000000000000000000001
fmt.Printf("16777219.0:%b\n", math.Float32bits(float32(16777219.0)))
fmt.Printf("16777220.0:%b\n", math.Float32bits(float32(16777220.0)))
fmt.Printf("16777221.0:%b\n", math.Float32bits(float32(16777221.0)))
//16777219.0:1001011100000000000000000000010
//16777220.0:1001011100000000000000000000010
//16777221.0:1001011100000000000000000000010

fmt.Printf("000:%f\n", math.Float32frombits(0b_10010111_00000000000000000000000))
// 000:16777216.000000
fmt.Printf("001:%f\n", math.Float32frombits(0b_10010111_00000000000000000000001))
// 001:16777218.000000
fmt.Printf("010:%f\n", math.Float32frombits(0b_10010111_00000000000000000000010))
// 010:16777220.000000
fmt.Printf("011:%f\n", math.Float32frombits(0b_10010111_00000000000000000000011))
// 011:16777222.000000

What are the rules?


Correct answer


go gives the correct ieee-754 binary floating point result - rounded to the nearest value, ties to even numbers.

go Programming Language Specification

float32     the set of all ieee-754 32-bit floating-point numbers

Decimal

16777219

is binary

1000000000000000000000011

For 32-bit ieee-754 floating point binary, the 24-bit integer mantissa is

100000000000000000000001.1

Round to the nearest value, even numbers are given

100000000000000000000010

Remove the implicit one bit of the 23-bit mantissa to get

00000000000000000000010
package main

import (
    "fmt"
    "math"
)

func main() {
    const n = 16777219
    fmt.printf("%d\n", n)
    fmt.printf("%b\n", n)
    f := float32(n)
    fmt.printf("%g\n", f)
    fmt.printf("%b\n", math.float32bits(f))
}

https://www.php.cn/link/f0bbf5fb2b067fda7b491dc2307411e4

16777219
1000000000000000000000011
1.677722e+07
1001011100000000000000000000010

The above is the detailed content of Go Float32bit() result is not expected. For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:stackoverflow.com. If there is any infringement, please contact admin@php.cn delete