/*
* Algorithms.java
*
* Created on: 2013.12.03
* Author: Wendy
*/
/*eclipse std kepler, jdk 1.7*/
public class Algorithms
{
public static double sqrt(double c)
{
if(c<0) return Double.NaN; //NaN: not a number
double err = 1e-15; //极小值
double t = c;
while (Math.abs(t-c/t) > err*t) //t^2接近c, 防止小数
t = (c/t + t)/2.0;
return t;
}
public static double cbrt(double c)
{
boolean b = (c>0) ? true : false; //保存c的符号
c = (c>0) ? c : -c;
double err = 1e-15;
double t = c;
while(Math.abs(t*t-c/t) > err*t)
t = (c/(t*t)+t)/2.0;
t = (b) ? t : -t;
return t;
}
public static void main(String[] args)
{
double r = sqrt(4.0);
System.out.println("sqrt(4.0) = " + r);
double rc = cbrt(-27.0);
System.out.println("cbrt(9.0) = " + rc);
}
}
sqrt(4.0) = 2.0 cbrt(9.0) = -3.0
机械节能产品生产企业官网模板...
大气智能家居家具装修装饰类企业通用网站模板...
礼品公司网站模板
宽屏简约大气婚纱摄影影楼模板...
蓝白WAP手机综合医院类整站源码(独立后台)...苏ICP备2024110244号-2 苏公网安备32050702011978号 增值电信业务经营许可证编号:苏B2-20251499 | Copyright 2018 - 2025 源码网商城 (www.ymwmall.com) 版权所有