cmath库中的sqrt函数 [英] sqrt function in cmath library

查看:672
本文介绍了cmath库中的sqrt函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在编写一个程序,它将使用方形的天花板和地板

root。


如果正整数是精确的正方形(并且是不是太大了),是否
sqrt返回整数值实数?


或者sqrt函数可能告诉我,例如,sqrt(1024) =

31.99999999999999999999999


您可能会说试试。问题是我无法无限地尝试很多情况。我一般都在询问平方数字。


我可以使用epsilon变量轻松编写一个程序,只有当它们将
数字整数转换为整数时在整数的epsilon内。

但是,这可能是不必要的 - 它可能已经内置在

sqrt函数中。


Paul Epstein

解决方案

pa ********** @ att.net 写道:

我正在编写一个程序,它将使用方形的天花板和地板
root。

如果一个正整数是一个精确的正方形(并且不是太大),
sqrt会返回一个整数值的实数吗?

或者可能sqrt函数告诉我,例如,sqrt(1024)=
31.99999999999999999999999

你可能会说试试。问题是我不能无限地尝试很多案件。我一般都在询问平方数。

我可以轻松地使用epsilon变量编写程序,只有当它们在整数的epsilon范围内时才会将数字舍入为整数。但是,这可能是不必要的 - 它可能已经内置在
sqrt函数中。




你不需要无限运行许多数字变化。 ''double''比''unsigned int''更多数字来存储尾数,最有可能。如果

你取UINT_MAX + 1的平方根,你就得到一个值。你只需要

来运行sqrt(UINT_MAX + 1)测试,大概是65536:


int r = -1;

for(int i = 0; i< 65536; i ++){

double d = i; //确切

r = sqrt(d * d);

if(r!= i)// bust!

break;

}


//在这里检查''r'

V

< br>

pa**********@att.net

我正在编写一个程序,它将使用正方形的天花板和地板。

如果正整数是一个精确的正方形(并且不是过度的()sqrt会返回一个整数值的实数吗?

也许sqrt函数告诉我,例如,sqrt(1024)=
31.99999999999999999999999


它可能会这样做。但是,问题不在于sqrt

函数,而是内部十进制数表示。我们昨天实际讨论了这个问题 - 搜索档案可能

帮助。

你可能会说尝试一下。问题是我不能无限地尝试很多案件。我一般都在询问平方数字。


您可以尝试一下,但是您只能了解它在您的系统上是如何工作的。

我可以使用epsilon变量轻松编写程序只有当它们在整数的epsilon范围内时才会将数字舍入为整数。
但是,这可能是不必要的 - 它可能已经内置在
sqrt函数中。




这可能是最稳定,最便携的解决方案。


Kristo

< br>

pa**********@att.net

我正在编写一个程序,它将使用正方形的天花板和地板。

如果正整数是精确的正方形(是不是太大了?
sqrt是否返回一个整数值的实数?

或者sqrt函数可能告诉我,例如,sqrt(1024)=
31.99999999999999999999999

你可能会说试试。问题是我不能无限地尝试很多案件。我一般都在询问平方数。

我可以轻松地使用epsilon变量编写程序,只有当它们在整数的epsilon范围内时才会将数字舍入为整数。但是,这可能是不必要的 - 它可能已经内置在
sqrt函数中。




好​​吧,据我所知C ++标准只是从C标准中整合了这些函数的语义

。我检查了我b / b
的草稿版本,我没有找到*任何*保证精度。

的格式是sqrt的C标准:


sqrt函数返回平方根的值。


这显然是一个谎言,因为一般来说,双子的平方根不是由双重表示的
。因此,对于所有人,我知道


双倍sqrt(双x){

返回0.0;

}


是符合标准的实现。这是一种实施质量

的事情。所以,也许你最好问你的图书馆供应商。


最好


Kai-Uwe Bux


I am writing a program which will use the ceiling and floor of square
roots.

If a positive integer is an exact square (and is not overly huge), does
sqrt return an integer-valued real?

Or might the sqrt function tell me that, for example, sqrt(1024) =
31.99999999999999999999999

You might say "try it". The problem is that I can''t try infinitely
many cases. I''m asking about square numbers in general.

I can easily write a program using an epsilon variable that will round
numbers to integers only if they are within epsilon of an integer.
However, this may be unnecessary -- it may already be built into the
sqrt function.

Paul Epstein

解决方案

pa**********@att.net wrote:

I am writing a program which will use the ceiling and floor of square
roots.

If a positive integer is an exact square (and is not overly huge), does
sqrt return an integer-valued real?

Or might the sqrt function tell me that, for example, sqrt(1024) =
31.99999999999999999999999

You might say "try it". The problem is that I can''t try infinitely
many cases. I''m asking about square numbers in general.

I can easily write a program using an epsilon variable that will round
numbers to integers only if they are within epsilon of an integer.
However, this may be unnecessary -- it may already be built into the
sqrt function.



You don''t need to run infinitely many number variations. ''double'' has
more digits than ''unsigned int'' to store the mantissa, most likely. If
you take the square root of UINT_MAX+1, you get a value. You only need
to run sqrt(UINT_MAX + 1) tests, which is probably 65536:

int r = -1;
for (int i = 0; i < 65536; i++) {
double d = i; // exact
r = sqrt(d*d);
if (r != i) // bust!
break;
}

// examine ''r'' here

V


pa**********@att.net wrote:

I am writing a program which will use the ceiling and floor of square
roots.

If a positive integer is an exact square (and is not overly huge), does
sqrt return an integer-valued real?

Or might the sqrt function tell me that, for example, sqrt(1024) =
31.99999999999999999999999
It just might do that. The problem, though, isn''t with the sqrt
function but with the internal representation of decimal numbers. We
actually discussed this just yesterday - searching the archives might
help.
You might say "try it". The problem is that I can''t try infinitely
many cases. I''m asking about square numbers in general.
You might try it, but you''d only find out how it works on your system.
I can easily write a program using an epsilon variable that will round
numbers to integers only if they are within epsilon of an integer.
However, this may be unnecessary -- it may already be built into the
sqrt function.



That''s probably the most consistent and portable solution.

Kristo


pa**********@att.net wrote:

I am writing a program which will use the ceiling and floor of square
roots.

If a positive integer is an exact square (and is not overly huge), does
sqrt return an integer-valued real?

Or might the sqrt function tell me that, for example, sqrt(1024) =
31.99999999999999999999999

You might say "try it". The problem is that I can''t try infinitely
many cases. I''m asking about square numbers in general.

I can easily write a program using an epsilon variable that will round
numbers to integers only if they are within epsilon of an integer.
However, this may be unnecessary -- it may already be built into the
sqrt function.



Well, as far as I can tell the C++ standard just incorporates the semantics
of these functions from the C standard. I checked the draft version that I
have, and I did not find *any* guarantees as to precision. The wording of
the C standard for sqrt is:

The sqrt functions return the value of the square root.

This, clearly is a lie since in general the square root of a double is not
representable by a double. Thus, for all, I know

double sqrt ( double x ) {
return 0.0;
}

is a standard compliant implementation. It is a "quality of implementation"
thing. So, maybe you are better off asking the vendor of your library.

Best

Kai-Uwe Bux


这篇关于cmath库中的sqrt函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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