Actionscript 3 平方根计算
想要计算给定输入的平方根,所以我使用了这段代码Actionscript 3 平方根计算,actionscript-3,apache-flex,flex4.5,Actionscript 3,Apache Flex,Flex4.5,想要计算给定输入的平方根,所以我使用了这段代码 var no1:Number = 2; var no2:Number; //input for calculation var total:Number; total=Math.pow(no2,(1/no1)); 这是可行的,但我面临的问题如下:- 如果我给 no2=25 然后就显现出来了 总计=4.9999999 为了克服这个问题,我使用了下面的代码 总计=Math.ceil(Math.pow(no2,(1/no1)) 但是25分钟就可以了 总
var no1:Number = 2;
var no2:Number; //input for calculation
var total:Number;
total=Math.pow(no2,(1/no1));
这是可行的,但我面临的问题如下:-
如果我给
no2=25
然后就显现出来了
总计=4.9999999
为了克服这个问题,我使用了下面的代码
总计=Math.ceil(Math.pow(no2,(1/no1))
但是25分钟就可以了
总数=5
问题是如果我给21,22,23,24
对于所有这些输入,它显示5
那么还有其他解决方案吗?
我的实际代码是这样的
var str:String=view.numDisplay.text;//input string
var power:Number = Number(inString.charAt(inString.indexOf('√') - 1));//to get the value before √ i.e no1
var no1:Number;
var no2:Number;
var total:Number;
if(power)
no1=v;
else
no1=2;
no2=getSubTerm();//method to find no2 it returns the number for calculation
total=Math.ceil(Math.pow(no2,(1/no1)));
如果你想取第n根。您可以将函数的输出输入任意舍入函数,如下所示:
/**
* Rounds input number to specified number of decimal places. For example
* round(4.568, 2) will return 4.57, and round(4.99, 2) will return 5.
*/
function round(num:Number, toDecimalPlaces:uint) {
var factor:uint = Math.pow(10, toDecimalPlaces);
return Math.round(num * factor) / factor;
}
/**
* Returns nth root to two decimal places.
*/
function nthRoot(n:uint, num:Number) {
return round(Math.pow(num, (1 / n)), 2);
}
是的,我知道,但我给no1作为变量,因为我想计算3√(二),四√(二),五√第二√(no2)只是一个双sqrt,所以
Math.sqrt(Math.sqrt(no2))
,4√(no2)是一个三重sqrt等,所以只需要做一个函数,迭代平方根的数字,并返回结果√25…四舍五入结果=8.881784197001252e-16…那么我如何将其转换为0.000000000000001@PeterI实际上建议使用第二种解决方案,而不是第一种解决方案。:)
/**
* Rounds input number to specified number of decimal places. For example
* round(4.568, 2) will return 4.57, and round(4.99, 2) will return 5.
*/
function round(num:Number, toDecimalPlaces:uint) {
var factor:uint = Math.pow(10, toDecimalPlaces);
return Math.round(num * factor) / factor;
}
/**
* Returns nth root to two decimal places.
*/
function nthRoot(n:uint, num:Number) {
return round(Math.pow(num, (1 / n)), 2);
}