數(shù)字可以是負數(shù)可以是浮點數(shù)奈偏,輸出的值最多保留1位小數(shù)
let num =19.9;
let low=0;
let heigh=Math.abs(num);
let mid = 0;
while(heigh-low>0.0001){
mid = (low+heigh)/2;
if( mid*mid*mid>Math.abs(num)){
heigh=mid
}else{
low = mid;
}
}
let result =num<0? -(low+heigh)/2:(low+heigh)/2
console.log(result.toFixed(1))