本文介绍了如何从字符串中手动解析浮点数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当然大多数语言都有用于此的库函数,但假设我想自己做.

Of course most languages have library functions for this, but suppose I want to do it myself.

假设浮点数是在 C 或 Java 程序中给出的('f' 或 'd' 后缀除外),例如4.2e1"、.42e2" 或简单的 "42".一般来说,我们有小数点前的整数部分"、小数点后的小数部分"和指数".三个都是整数.

Suppose that the float is given like in a C or Java program (except for the 'f' or 'd' suffix), for example "4.2e1", ".42e2" or simply "42". In general, we have the "integer part" before the decimal point, the "fractional part" after the decimal point, and the "exponent". All three are integers.

查找和处理单个数字很容易,但是如何将它们组合成 floatdouble 类型的值而不损失精度?

It is easy to find and process the individual digits, but how do you compose them into a value of type float or double without losing precision?

我正在考虑将整数部分与 10^n 相乘,其中 n 是小数部分的位数,然后将小数部分添加到整数部分并从指数中减去 n.例如,这有效地将 4.2e1 转换为 42e0.然后我可以使用 pow 函数来计算 10^exponent 并将结果与​​新的整数部分相乘.问题是,这种方法是否能保证整个过程中的最大精度?

I'm thinking of multiplying the integer part with 10^n, where n is the number of digits in the fractional part, and then adding the fractional part to the integer part and subtracting n from the exponent. This effectively turns 4.2e1 into 42e0, for example. Then I could use the pow function to compute 10^exponent and multiply the result with the new integer part. The question is, does this method guarantee maximum precision throughout?

对此有什么想法吗?

推荐答案

我会直接使用浮点数的二进制表示来组合浮点数.

I would directly assemble the floating point number using its binary representation.

一个接着一个读入数字,首先找出所有数字.在整数算术中做到这一点.还要跟踪小数点和指数.这个以后会很重要.

Read in the number one character after another and first find all digits. Do that in integer arithmetic. Also keep track of the decimal point and the exponent. This one will be important later.

现在您可以组合您的浮点数.首先要做的是扫描数字的整数表示,以获取第一组一位(从高到低).

Now you can assemble your floating point number. The first thing to do is to scan the integer representation of the digits for the first set one-bit (highest to lowest).

紧跟在第一个位之后的位是您的尾数.

The bits immediately following the first one-bit are your mantissa.

获得指数也不难.您知道第一个一位的位置、小数点的位置和科学记数法中的可选指数.组合它们并添加浮点指数偏差(我认为它是 127,但请检查一些参考).

Getting the exponent isn't hard either. You know the first one-bit position, the position of the decimal point and the optional exponent from the scientific notation. Combine them and add the floating point exponent bias (I think it's 127, but check some reference please).

这个指数应该在 0 到 255 范围内的某个地方.如果它更大或更小,你就有一个正数或负数(特殊情况).

This exponent should be somewhere in the range of 0 to 255. If it's larger or smaller you have a positive or negative infinite number (special case).

将指数存储到浮点数的 24 到 30 位.

Store the exponent as it into the bits 24 to 30 of your float.

最重要的位就是符号.一表示负数,零表示正数.

The most significant bit is simply the sign. One means negative, zero means positive.

这比实际情况更难描述,尝试分解一个浮点数并查看指数和尾数,您会发现它真的很容易.

It's harder to describe than it really is, try to decompose a floating point number and take a look at the exponent and mantissa and you'll see how easy it really is.

顺便说一句 - 在浮点本身中进行算术运算是一个坏主意,因为您总是会强制将尾数截断为 23 个有效位.那样你不会得到准确的表示.

Btw - doing the arithmetic in floating point itself is a bad idea because you will always force your mantissa to be truncated to 23 significant bits. You won't get a exact representation that way.

这篇关于如何从字符串中手动解析浮点数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-24 10:21