为什么Math.Sqrt不采用小数? [英] Why does Math.Sqrt not take a decimal?
问题描述
为什么Math.Sqrt()只接受double作为参数?我认为
它会对小数(或int,或float,或......)感到满意。我可以轻松地来回转换
,但我对幕后发生的事情感兴趣
场景,如果有小数点的某些方面使它们不能
用于此计算。
谢谢!
Ethan
Ethan Strauss博士<生物信息学科学家
Promega公司
2800 Woods Hollow Rd。
麦迪逊,WI 53711
608-274-4330
800-356-9526
et ***********@promega.com
Ethan Strauss< Et ***** *****@discussions.microsoft.comwrote:
为什么Math.Sqrt()只接受double作为参数?我认为
它会对小数(或int,或float,或......)感到满意。我可以轻松地来回转换
,但我对幕后发生的事情感兴趣
场景,如果有小数点的某些方面使它们不能
用于此计算。
嗯,通常在处理数字时使用小数
这些数字自然而且*正好*表示为小数 - 货币
是最常见的例子。
你可能采取的平方根的种类是那种
你应该使用双倍代替。
-
Jon Skeet - < sk *** @ pobox.com>
网站: http://www.pobox.com/~双向飞碟
博客: http:// www .msmvps.com / jon.skeet
C#深度: http: //csharpindepth.com
Ethan Strauss写道:
为什么Math.Sqrt( )只接受一个double作为参数?我认为
它会对小数(或int,或float,或......)感到满意。我可以轻松地来回转换
,但我对幕后发生的事情感兴趣
场景,如果有小数点的某些方面使它们不能
用于此计算。
通常这种类型的函数返回的值与
类型相同。
小数的Sqrt不会返回真正的小数(小数是
应该是准确的)。
int的Sqrt肯定不会返回int 。
双Sqrt(十进制)和双Sqrt(int)不会跟随
练习功能
十进制Sqrt(十进制)和int Sqrt(int)将给出一个关于结果不正确的
印象
此外:你何时需要服用平方根87美元??
:-)
Arne
Math类使用的所有变量都是原始类型。
CLR不认为Decimal是原始类型(CLR不是
包含处理小数类型的特殊IL指令)。我会想象
这是理由之一吗?也许???
你可以查看一个Decimal的定义,你会看到它实现/覆盖它上面的所有数学运算符包括但是
不限于+, - ,*,/,圆形,天花板,地板等等,其中许多也包含在Math类中但是包含在Decimal类型中的
提供了自己的
实现。
" Ethan Strauss" < Et ********** @ discussion.microsoft.com写信息
news:77 ****************** **************** @ microsof t.com ...
为什么Math.Sqrt()只接受double作为参数?我认为
它会对小数(或int,或float,或......)感到满意。我好b $ b可以
轻松来回转换,但我感兴趣的是
落后
场景,如果有小数点的某些方面让他们免于在这个计算中使用
。
谢谢!
Ethan
Ethan Strauss博士
生物信息学科学家
Promega公司
2800 Woods Hollow Rd。
麦迪逊,WI 53711
608-274-4330
800-356-9526
et *********** @ promega.com
Hi,
Why does Math.Sqrt() only accept a double as a parameter? I would think
it would be just as happy with a decimal (or int, or float, or ....). I can
easily convert back and forth, but I am interested in what is going on behind
the scenes and if there is some aspect of decimals that keep them from being
used in this calculation.
Thanks!
Ethan
Ethan Strauss Ph.D.
Bioinformatics Scientist
Promega Corporation
2800 Woods Hollow Rd.
Madison, WI 53711
608-274-4330
800-356-9526
et***********@promega.com
Ethan Strauss <Et**********@discussions.microsoft.comwrote:Why does Math.Sqrt() only accept a double as a parameter? I would think
it would be just as happy with a decimal (or int, or float, or ....). I can
easily convert back and forth, but I am interested in what is going on behind
the scenes and if there is some aspect of decimals that keep them from being
used in this calculation.Well, decimals are typically used when you''re dealing with numbers
which are naturally and *exactly* represented as decimals - currency
being the most common example.
The kind of number you''re likely to take the square root of is the kind
of number you should probably be using double for instead.
--
Jon Skeet - <sk***@pobox.com>
Web site: http://www.pobox.com/~skeet
Blog: http://www.msmvps.com/jon.skeet
C# in Depth: http://csharpindepth.com
Ethan Strauss wrote:Why does Math.Sqrt() only accept a double as a parameter? I would think
it would be just as happy with a decimal (or int, or float, or ....). I can
easily convert back and forth, but I am interested in what is going on behind
the scenes and if there is some aspect of decimals that keep them from being
used in this calculation.Usually that type of functions returns a value of the same
type as itself.
Sqrt of a decimal will not return a true decimal (decimal is
supposed to be exact).
Sqrt of an int will definitely not return an int.
double Sqrt(decimal) and double Sqrt(int) will not follow
practice for functions
decimal Sqrt(decimal) and int Sqrt(int) will give an
impression about the result that is not correct
Besides: when do you need to take the square root of 87 dollars ??
:-)
Arne
All of the variables used by the Math class are primitive types.
The CLR does not consider a Decimal to be a primitive type (the CLR does not
contain special IL instructions to handle decimal types). I would imagine
that is one of the reasona?|. maybe???
You can check out the definition of a Decimal and you will see that it
implements/overrides just about all the math operators on it including but
not limited to +,-,*,/,Round, Ceiling, Floor etc, many of this are also
included on the Math class but the Decimal type provides its own
implementation.
"Ethan Strauss" <Et**********@discussions.microsoft.comwrote in message
news:77**********************************@microsof t.com...Hi,
Why does Math.Sqrt() only accept a double as a parameter? I would think
it would be just as happy with a decimal (or int, or float, or ....). I
can
easily convert back and forth, but I am interested in what is going on
behind
the scenes and if there is some aspect of decimals that keep them from
being
used in this calculation.
Thanks!
Ethan
Ethan Strauss Ph.D.
Bioinformatics Scientist
Promega Corporation
2800 Woods Hollow Rd.
Madison, WI 53711
608-274-4330
800-356-9526
et***********@promega.com
这篇关于为什么Math.Sqrt不采用小数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!