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

Go Float32bit() result is not expected

WBOY
Release: 2024-02-06 09:25:11
forward
1179 people have browsed it

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
Copy after login

but:

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

// 1001011100000000000000000000010
Copy after login

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
Copy after login

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
Copy after login

Decimal

16777219
Copy after login

is binary

1000000000000000000000011
Copy after login

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

100000000000000000000001.1
Copy after login

Round to the nearest value, even numbers are given

100000000000000000000010
Copy after login

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

00000000000000000000010
Copy after login
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))
}
Copy after login

//m.sbmmt.com/link/f0bbf5fb2b067fda7b491dc2307411e4

16777219
1000000000000000000000011
1.677722e+07
1001011100000000000000000000010
Copy after login

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!

source:stackoverflow.com
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template