简体   繁体   English

如何从十进制数转换为IEEE 754单精度浮点格式?

[英]How do I convert from a decimal number to IEEE 754 single-precision floating-point format?

How would I go about manually changing a decimal (base 10) number into IEEE 754 single-precision floating-point format? 如何手动将十进制(基数为10)的数字转换为IEEE 754单精度浮点格式? I understand that there is three parts to it, a sign, an exponent, and a mantissa. 我知道它有三个部分,一个符号,一个指数和一个尾数。 I just don't completely understand what the last two parts actually represent. 我只是不完全理解最后两个部分实际代表什么。

Find the largest power of 2 which is smaller than your number, eg if you start with x = 10.0 then 2 3 = 8, so the exponent is 3. The exponent is biased by 127 so this means the exponent will be represented as 127 + 3 = 130. The mantissa is then 10.0/8 = 1.25. 找到小于你的数字的2的最大幂,例如,如果你从x = 10.0开始然后2 3 = 8,那么指数是3.指数偏向127,这意味着指数将表示为127 + 3 = 130.然后尾数为10.0 / 8 = 1.25。 The 1 is implicit so we just need to represent 0.25, which is 010 0000 0000 0000 0000 0000 when expressed as a 23 bit unsigned fractional quantity. 1是隐式的,因此我们只需要表示0.25,即表示为23位无符号小数的010 0000 0000 0000 0000 0000。 The sign bit is 0 for positive. 符号位为0表示正数。 So we have: 所以我们有:

s | exp [130]  | mantissa [(1).25]            |

0 | 100 0001 0 | 010 0000 0000 0000 0000 0000 |

0x41200000

You can test the representation with a simple C program, eg 您可以使用简单的C程序测试表示,例如

#include <stdio.h>

typedef union
{
    int i;
    float f;
} U;

int main(void)
{
    U u;

    u.f = 10.0;

    printf("%g = %#x\n", u.f, u.i);

    return 0;
}

Take a number 172.625.This number is Base10 format. 取一个数字172.625.This数字是Base10格式。

Convert this format is in base2 format For this, first convert 172 in to binary format 转换此格式为base2格式为此,首先将172转换为二进制格式

128 64 32 16 8 4 2 1
 1  0  1  0  1 1 0 0
172=10101100

Convert 0.625 in to binary format 将0.625转换为二进制格式

0.625*2=1.250   1
0.250*2=.50     0
0.50*2=1.0      1
0.625=101

Binary format of 172.625=10101100.101. 二进制格式172.625 = 10101100.101。 This is in base2 format 10101100*2 这是base2格式10101100 * 2

Shifting this binary number 移动此二进制数

1.0101100*2 **7      Normalized
1.0101100 is mantissa
2 **7 is exponent

add exponent 127 7+127=134 添加指数127 7 + 127 = 134

convert 134 in to binary format 将134转换为二进制格式

134=10000110

The number is positive so sign of the number 0 数字为正数,因此数字为0

0 |10000110 |01011001010000000000000

Explanation: The high order of bit is the sign of the number. 说明:位的高位是数字的符号。 number is stored in a sign magnitude format. 数字以符号幅度格式存储。 The exponent is stored in 8 bit field format biased by 127 to the exponent The digit to the right of the binary point stored in the low order of 23 bit. 指数以8位字段格式存储,偏向127指数。二进制点右侧的数字以23位的低位存储。 NOTE---This format is IEEE 32 bit floating point format 注意---这种格式是IEEE 32位浮点格式

A floating point number is simply scientific notation . 浮点数只是科学记数法 Let's say I asked you to express the circumference of the Earth in meters , using scientific notation. 让我们说我用科学记数法要求你以为单位表示地球周长 You would write: 你会写:

4.007516×10 7 m 4.007516×10 7

The exponent is just that: the power of ten here. 指数只是:这里十的力量。 The mantissa is the actual digits of the number. 尾数是数字的实际数字。 And the sign, of course, is just positive or negative. 当然,这个标志只是正面或负面的。 So in this case the exponent is 7 and the mantissa is 4.007516 . 因此,在这种情况下,指数为7,尾数为4.007516。

The only significant difference between IEEE754 and grade-school scientific notation is that floating point numbers are in base 2 , so it's not times ten to the power of something, it's times two to the power of something. IEEE754与小学 - 科学记数法之间唯一显着的区别是浮点数在基数2中 ,所以它不是某事物能力的十倍,它是某事物能力的两倍。 So where you would write, say, 256 in ordinary human scientific notation as: 那么你可以用普通的人类科学记数法写出256,如:

2.56×10 2 (mantissa 2.56 and exponent 2), 2.56×10 2 (尾数2.56和指数2),

in IEEE754, it's 在IEEE754中,它是

1×2 8 — the mantissa is 1 and the exponent is 8. 1×2 8 - 尾数为1,指数为8。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何将IEEE 754单精度浮点格式转换为十进制? - How do you convert from IEEE 754 single-precision floating-point format to decimal? 如何将IEEE 754单精度二进制浮点数转换为十进制? - How to convert an IEEE 754 single-precision binary floating-point to decimal? 将十进制转换为单精度IEEE 754 - Converting Decimal to Single-Precision IEEE 754 如何从 .NET 的 BinaryWriter 序列化的四个字节中读取单精度浮点数 - How to read a single-precision floating-point number from four bytes serialized by .NET's BinaryWriter 将十进制数转换为浮点表示法和 IEEE 754 格式 - Converting a decimal number to floating point notation and IEEE 754 format 十进制到 IEEE 754 使用 C 的单精度 IEEE 754 代码 - Decimal to IEEE 754 Single-precision IEEE 754 code using C 如何将 DEC 64 位双精度浮点数转换为 IEEE-754(DEC 不是十进制) - How to convert DEC 64bit double precision floating point to IEEE-754 (DEC is not decimal) MATLAB:将uint32(4字节)值转换为相应的IEEE单精度浮点形式 - MATLAB: Converting a uint32 (4-byte) value to the corresponding IEEE single-precision floating-point form 如何将2+(2/7)转换为IEEE 754浮点 - How to convert 2+(2/7) to IEEE 754 floating point 将十六进制转换为IEEE-754单精度浮点二进制科学计数法 - Convert hexadecimal to IEEE-754 single precision floating point binary scientific notation
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM