简体   繁体   中英

c++ stof() function wrong after working multiple times

I have written a function that reads an obj. file and converts the strings into floats. The code worked fine untill the stof() function stops working properly. 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. I use fstream and iterators to find the numbers. 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.

# 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. So when stemp holds "0.000100" ftemp should hold 0.000100 yet it holds: 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.

9.9999997e-005 is 0.00000999999997 so its close enough.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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