检测很长的浮子? [英] detecting very long floats?

查看:50
本文介绍了检测很长的浮子?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述




使用scanf,是否可以检测小数

数太大的输入数字?

例如。


scanf("%19lg"& var);


(如果

1.123456789101212314515324234132132

被放入,它会返回一定的错误值吗?


任何建议都非常感谢....


干杯,

Andrew

Hi,

Using scanf, is it possible to detect input numbers where the decimal
count is too large?
eg.

scanf("%19lg",&var);

(and if
1.123456789101212314515324234132132
was put in, it would return a certain error value?

Any advice greatly appreciated....

Cheers,
Andrew

推荐答案

AndrewD写道:
AndrewD wrote:


使用scanf,是否可以检测小数
数太大的输入数?

例如。

scanf(" ;%19lg"& var);

(如果放入,它会返回一定的错误值吗?

任何建议都非常感谢....
Hi,

Using scanf, is it possible to detect input numbers where the decimal
count is too large?
eg.

scanf("%19lg",&var);

(and if
1.123456789101212314515324234132132
was put in, it would return a certain error value?

Any advice greatly appreciated....




有几种可能性:

- 使用%n来查找已经有多少字符d

- 使用扫描集格式查看我们是否继续使用有效的

字符(例如"%[\ t \ n]")并检查返回值

scanf()

- 使用反向扫描集格式来查看我们没有继续使用

无效字符(例如%[^ 0-9eE。+ - ])并检查返回

scanf()的值


检查scanf()和朋友_always_的返回值是

a好主意。

scanf()并不总是有效意;看看fgets()和sscanf()的组合是否更适合你的目的。

干杯

Michael

-

电子邮件:我的是/ at / gmx / dot / de地址。



There are several possibilities:
- Use %n to find out how many characters have been read
- Use a scanset format to see whether we continue with valid
characters (e.g. "%[ \t\n]") and check the return value of
scanf()
- Use an inverse scanset format to see that we don''t continue with
invalid characters (e.g "%[^0-9eE.+-]") and check the return
value of scanf()

Checking the return value of scanf() and friends _always_ is
a good idea.
scanf() does not always work as intended; see whether a combination
of fgets() and sscanf() does better suit your purposes.
Cheers
Michael
--
E-Mail: Mine is an /at/ gmx /dot/ de address.


AndrewD< an ** ***@cyberone.com.au>写道:
AndrewD <an*****@cyberone.com.au> wrote:
使用scanf,是否可以检测小数
数太大的输入数字?
scanf("%19lg",& var);

(如果放入,它会返回一定的错误值?
Using scanf, is it possible to detect input numbers where the decimal
count is too large? scanf("%19lg",&var);

(and if
1.123456789101212314515324234132132
was put in, it would return a certain error value?




不会。它会读取所需的数字,并在此时保留输入

流。后续读取将获得下一个数字。如果你想要这个功能,一个好方法是使用fgets()读取整行(或者你需要的多少
),然后在它上面使用strtod()。


Richard



No. It would read as many digits as it needed, and leave the input
stream at that point. A subsequent read would get the next digit. If you
want this feature, a good way is to read a whole line (or as much of it
as you need) using fgets(), and then use strtod() on it.

Richard


AndrewD写道:
AndrewD wrote:

使用scanf,是否可以检测到十进制数太大的输入数字?

例如.scanf("%19lg"& var);

(如果输入了1.123456789101212314515324234132132
,它会返回一定的错误值吗?

Using scanf, is it possible to detect input numbers where the
decimal count is too large?

eg. scanf("%19lg",&var);

(and if 1.123456789101212314515324234132132
was put in, it would return a certain error value?




单个安装无法回答,但据我所知

关注任何东西t输入来自流的浮点值

应接受所有适合的输入,即使它会被忽略

为无关紧要。所有浮点数包含的是实际值的近似值b / b
,不受那些后来的数字的影响。一旦检测到满意的值并且输入了小数点b / b,就不可能在没有指数子句(eNNN)的情况下进一步下溢

或溢出其他数字。


正确处理所有可能的数字文本输入的例程

并非无足轻重。有关示例,请参阅

txtinput.zip中的函数readxreal,编写为在Turbo / Borland Pascal下提供

标准Pascal的一些功能,可从以下位置获取:


< http://cbfalconer.home.att.net/download/txtfiles.zip>


-

"如果您想通过groups.google.com发布跟进,请不要使用

破损的回复链接在文章的底部。点击

" show options"在文章的顶部,然后点击

回复在文章标题的底部。 - Keith Thompson



I can''t answer for individual installations, but as far as I am
concerned anything that inputs floating point values from a stream
should accept all input that fits, even though it will be ignored
as insignificant. All a floating point number contains is an
approximation to the actual value, which is not affected by those
later digits. Once a satisfactory value has been detected and a
decimal point input, it is impossible to have a further underflow
or overflow on additional digits without an exponent clause (eNNN).

A routine to properly handle all possible numerical text input is
not trivial. For an example see the function readxreal in
txtinput.zip, written to supply some of the capabilities of
standard Pascal under Turbo/Borland Pascal, and available at:

<http://cbfalconer.home.att.net/download/txtfiles.zip>

--
"If you want to post a followup via groups.google.com, don''t use
the broken "Reply" link at the bottom of the article. Click on
"show options" at the top of the article, then click on the
"Reply" at the bottom of the article headers." - Keith Thompson


这篇关于检测很长的浮子?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆