Hi talents, I have noticed that atof() function approximates the string I pass toit. when I use atof() , as atof(" 184.64")and it returns 184.63999999999 But I would like to have the exact value that is passed. Could anyone help me? ThanksSuresh Ooty 推荐答案 ok ****** @ gmail.com 写道: ok******@gmail.com wrote: 嗨人才, 我注意到atof()函数近似于我传递给的字符串它。 当我使用atof()时,作为atof(184.64)它返回184.63999999999 但我想要准确的传递的价值。 任何人都可以帮助我吗? Hi talents, I have noticed that atof() function approximates the string I pass to it. when I use atof() , as atof(" 184.64") and it returns 184.63999999999 But I would like to have the exact value that is passed. Could anyone help me? 否。浮点类型不是确切的类型。它们不能代表每个 的可能值。因此,无论何时将字符串 的数字表示转换为浮点值,或者采用 浮点值的字符串表示,都必须进行一些近似。 br /> No. Floating point types are not exact types. They cannot represent everypossible value. So whenever you convert a number representation of a stringinto a floating point value, or take the string representation of afloating point value, some approxiamtion must be done. >当我使用atof()时,作为atof(184.64)> when I use atof() , as atof(" 184.64")并返回184.63999999999 但我希望得到传递的确切值。and it returns 184.63999999999But I would like to have the exact value that is passed. 对于浮点运算,您应该指定一些容忍度,例如 EPSILON。 顺便说一下,即使是相同的数字,可以用两种不同的表示来表示,如果你有:double a = 10.5,b = 10.5;它很可能是一个!= b; Alaways使用:if(fabs(a - b)< EPSILON)然后它们是等于的。 For floating point operations you should specify some toleration likeEPSILON.By the way, even the same number, can be represented in two differentrepresentation, that it: if you have: double a = 10.5, b = 10.5; it''svery probable that a != b;Alaways use: if( fabs ( a - b ) < EPSILON ) then they are equals. ok ****** @ gmail.com 写道:我注意到atof()函数近似于我传递给它的字符串 当我使用atof()时,atof( 184.64)它返回184.63999999999 I have noticed that atof() function approximates the string I pass to it. when I use atof() , as atof(" 184.64") and it returns 184.63999999999 真的吗?试试这个: #include< stdio.h> #include< stdlib.h> int main( ){ double d = atof(" 184.64"); printf("%f\ n",d); } 适合我... V - 请在通过电子邮件回复时删除资金''A' 我没有回复最热门的回复,请不要问 Really? Try this: #include <stdio.h>#include <stdlib.h>int main() {double d = atof(" 184.64");printf("%f\n", d);} Works for me... V--Please remove capital ''A''s when replying by e-mailI do not respond to top-posted replies, please don''t ask 这篇关于atof近似于String,我需要传递的字符串的确切值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!
11-03 04:41