简体   繁体   English

多次工作后c ++ stof()函数错误

[英]c++ stof() function wrong after working multiple times

I have written a function that reads an obj. 我写了一个函数来读取一个obj。 file and converts the strings into floats. file并将字符串转换为浮点数。 The code worked fine untill the stof() function stops working properly. 代码工作正常,直到stof()函数停止正常工作。 I'm not sure how this part suddenly stops working since it seems such a straight-forward thing to do and it has worked everytime untill now. 我不确定这部分是如何突然停止工作的,因为这似乎是一件非常直接的事情,而且它现在每次都有效。

ftemp = std::stof(stemp);

ftemp and stemp (storage for float and string) are declared at the beginning of my function and are overwritten for each number. ftemp和stemp(float和string的存储)在我的函数开头声明,并为每个数字覆盖。 I use fstream and iterators to find the numbers. 我使用fstream和迭代器来查找数字。 The rest of the code is easy to imagine and works as expected so I'll ommit everything but the failing part. 代码的其余部分很容易想象并按预期工作,所以除了失败部分之外我会省略所有内容。

this is in the .obj file and should represent a textured plane. 这是在.obj文件中,应该代表一个纹理平面。

# Blender v2.68 (sub 0) OBJ File: ''
# www.blender.org
v -1.000000 1.000000 -1.000000
v 1.000000 1.000000 -1.000000
v -1.000000 1.000000 1.000000
v 1.000000 1.000000 1.000000
vt 0.000100 0.000100
vt 0.999900 0.000100
vt 0.000100 0.999900
vt 0.999900 0.999900
s off
f 2/1 1/2 4/3
f 1/2 3/4 4/3

It fails at the point it reads the vt lines. 它在读取vt线时失败了。 So when stemp holds "0.000100" ftemp should hold 0.000100 yet it holds: 9.9999997e-005. 因此,当stemp持有“0.000100”时,ftemp应保持0.000100但它仍然存在:9.9999997e-005。 The rest works the same way and does it correctly. 其余的工作方式相同,并且正确。

Any help would be greatly appreciated. 任何帮助将不胜感激。

It's correct. 这是正确的。 IEEE754 for single precision which float implements, guarantees accuracy up to around 7 significant digits. IEEE754用于float实现的单精度,保证精度高达约7位有效数字。

9.9999997e-005 is 0.00000999999997 so its close enough. 9.9999997e-0050.00000999999997所以它足够接近。

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM