如何判断数字的小数位数是否不等于0 [英] How to tell if a number has decimal places different than 0

查看:229
本文介绍了如何判断数字的小数位数是否不等于0的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

每次我发现娱乐节目挑战时迟早都会发现我是如何测试一个数字是否小数位数不等于

0?


例如,如果我想知道一个数字是否是一个正方形数字(即

数字,其中平方根是正数,为4,9,16有)我做

类似于:


int square = sqrt(number);


if((int)square = =方形)

//数字是一个完美的正方形

其他

//数字不是一个完美的正方形

是否有功能或语言特定方式来执行此操作?

Sooner or later everytime I found recreational programming challenges I
stumble with how I test if a number is has decimal places differnt than
0?

For example if I want to know if a number is a square number (i.e. a
number which square root is a positive number as 4, 9, 16 have) I do
something like:

int square = sqrt(number);

if((int)square==square)
// number is a perfect square
else
// number is not a perfect square

Is there a function or a language-specific-way to do this?

推荐答案

Gaijinco在26 /写道09/05:
Gaijinco wrote on 26/09/05 :
每次我发现娱乐节目挑战时迟早都会发现我如何测试一个数字是否有小数位数不等于
0?

例如,如果我想知道数字是否是正方形数字(即
平方根为正数的数字为4,9,16有)我这样做:

int square = sqrt(number);
if((int)square == square)
//数字是一个完美的正方形
其他
//数字不是一个完美的正方形

有没有功能或语言特定的方式吗?
Sooner or later everytime I found recreational programming challenges I
stumble with how I test if a number is has decimal places differnt than
0?

For example if I want to know if a number is a square number (i.e. a
number which square root is a positive number as 4, 9, 16 have) I do
something like:

int square = sqrt(number);

if((int)square==square)
// number is a perfect square
else
// number is not a perfect square

Is there a function or a language-specific-way to do this?




fmod()


-

Emmanuel

C-FAQ: http://www.eskimo.com/~scs/C-faq/faq.html

C库: http://www.dinkumware.com/refxc.html


它是指定的。但任何编写这样代码的人都应该将这些代码转化为蚯蚓并喂给鸭子。 - Chris Dollin CLC



fmod()

--
Emmanuel
The C-FAQ: http://www.eskimo.com/~scs/C-faq/faq.html
The C-library: http://www.dinkumware.com/refxc.html

"It''s specified. But anyone who writes code like that should be
transmogrified into earthworms and fed to ducks." -- Chris Dollin CLC


Gaijinco写道:
Gaijinco wrote:
每次我发现娱乐节目挑战时迟早都会因为我如何测试而磕磕绊绊如果一个数字的小数位数不同于
0?

例如,如果我想知道一个数字是否是一个平方数(即一个平方根是一个数字的数字)正数为4,9,16有)我这样做:

int square = sqrt(数字);

if((int)square = =方形)
//数字是一个完美的正方形
其他
//数字不是一个完美的正方形

是否有函数或语言特定的方式这样做?
Sooner or later everytime I found recreational programming challenges I
stumble with how I test if a number is has decimal places differnt than
0?

For example if I want to know if a number is a square number (i.e. a
number which square root is a positive number as 4, 9, 16 have) I do
something like:

int square = sqrt(number);

if((int)square==square)
// number is a perfect square
else
// number is not a perfect square

Is there a function or a language-specific-way to do this?




语言特定?这是什么意思?


你的测试并不完美,因为当square大于

时,它会失败。此测试始终有效(在
浮点精度的限制范围内)。


#include< math>


double square = sqrt(number);

if(floor(square)== square)

//数字是一个完美的正方形

其他

//数字不是一个完美的正方形


john



Language-specific? What do you mean by that?

Your test is not perfect because it will fail when square is bigger than
the biggest int. This test always works (within the limitations of
floating point accuracy).

#include <math>

double square = sqrt(number);
if (floor(square) == square)
// number is a perfect square
else
// number is not a perfect square

john





Gaijinco在09/26/5 16:07写道:


Gaijinco wrote On 09/26/05 16:07,:
每次我发现娱乐节目挑战时迟早会发现我如何测试数字是否有小数位数不等于
0?

例如,如果我想知道数字是否是正方形数字(即
数字,平方根是正数如4,9,16有)我做的事情如下:

int square = sqrt(number);

if((int)square == square )
//数字是一个完美的正方形


....而且测试是重言式。

其他
//号码是不是一个完美的广场

是否有功能或语言特定的方式来做到这一点?
Sooner or later everytime I found recreational programming challenges I
stumble with how I test if a number is has decimal places differnt than
0?

For example if I want to know if a number is a square number (i.e. a
number which square root is a positive number as 4, 9, 16 have) I do
something like:

int square = sqrt(number);

if((int)square==square)
// number is a perfect square
.... and the test is a tautology.
else
// number is not a perfect square

Is there a function or a language-specific-way to do this?




你已经显示的代码将(如果它没有调用

未定义的行为)声明每个数字都是

完美的正方形:1,2,3.14,甚至-42。 />

要测试一个浮点数是否为没有小数部分的

整数,你可以试试


如果((int)fpn == fpn)...

当fpn

的幅度很大时会遇到麻烦,这个值太大了超出范围

数字可表示为int'。


作为改进,您可以尝试


if(fmod(fpn,1.0)== 0.0)...


这仍然容易受到粒度的影响。

浮点数,这不是数学真实的

无限精确的数字。


你通常不能指望sqrt( fpn)是fpn的确切方格

的根。 sqrt(fpn)将非常接近确切的根,

但是(通常)与

真值略有不同。

可能有几个不同的fpn值,sqrt(fpn)会提供完全相同的稍微错误

答案:sqrt(4.0)和sqrt(4.0 + tiny_number)可能

产生2.0作为答案。如果你决定一个数字是一个完美的正方形,如果它的计算平方根结果是一个

整数,你会错误地断定4.0 + tiny_number是

a完美的正方形。


一个可能更彻底的测试可能会计算方块

root,测试它是否是整数,然后测试是否

其平方等于原始数字:


double root = sqrt(number);

if(fmod(root) ,1.0)== 0.0&& root * root == number)


....但即使这可能有一些问题。我总是感到不安

比较浮点数量是否完全相等,

主要是因为fpn'通常被视为近似值

to首先。你通常需要某种类型的近似相等的b $ b测试,这样的测试并不适合

完全平方的纯粹性质/无性质。


对于数论计算你通常会更好地使用某些风味的整数。如果数字增长

大,你可能需要求助于bignum。包;几个

可用。


-
Er ********* @ sun.com


这篇关于如何判断数字的小数位数是否不等于0的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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