我需要用Java创建一个程序来计算函数: cbrt(1 +x²)-x
具有最小的相对误差。但我不知道这个的逻辑,任何人都可以帮助我吗?
(抱歉我的英语不好)
答案 0 :(得分:0)
使用Java的内置数学库。
double myCalculation(double x) {
return Math.pow(1.0 + Math.pow(x, 2.0), (1.0/3.0)) - x;
}
Math位于java.lang
包中,因此它是Java母语的一部分;没有涉及第三方库。 Math.pow(double x, double y)需要两个双精度数--x和y - 并返回x ^ y的双重表示。
请注意,使用此方法,您仍需要担心浮点舍入错误。这对您正在进行的计算类型是否重要,由您决定。
答案 1 :(得分:0)
如果//API Method
export function updateSchedulecalendar(req, res) {
var responseSchedule;
//Insert
return Engineer.findOneAndUpdate({ _id: req.params.id }, { $addToSet: { schedule: req.body } }, { new: true, upsert: true, setDefaultsOnInsert: true, runValidators: true }).exec()
.then((entity) => {
if (entity) {
responseSchedule = entity.schedule;
return EngineerEvents.emit(engineerEvents.updatedSchedule, req.user, entity);
}
else {
return res.status(404).end();
}
})
.then(()=> { return res.status(200).json(responseSchedule); })
.catch(handleError(res));
}
不足以计算多维数据集根并且您想要更高的精度,则可以使用Math.pow
尽可能接近正确。你可以使用牛顿法或哈雷法,直到你足够近。