BigDecimal 符号/括号
我正在尝试强制 BigDecimal 使用符号(例如,不使用 Decimal.multiply,而是执行 Decimal *),因为此问题涉及大量括号。
你们能告诉我是否有一种方法可以在 BigDecimal 上使用符号而不将其转换为 Double 或其他东西?或者可以将 t 转换成 term 之类的格式吗?
double t = ((1.00 / f1) * ((((4.00 / (ak1 + 1.00)) - (2.00 / (ak1 + 4.00))) - (1.00 / (ak1 + 5.00))) - (1 / (ak1 + 6.00))));
对于像这样的格式,
term = ((one.divide(f,mc)).multiply(((((four.divide((ak.add(one)),mc)).subtract((two.divide((ak.add(four)),mc)))).subtract((one.divide((ak.add(five)),mc)))).subtract((one.divide((ak.add(six)),mc))))));
我已经多次尝试记录它,并花了近 6 个小时试图找出 BigDecimal 的错误所在。
I'm trying force BigDecimal to use symbols (Ex. Instead of Decimal.multiply, do Decimal *), because of the mass parenthesis being involved in this problem.
Can you guys tell me if there's a way to use symbols on BigDecimal without converting it to Double or something? Or can you convert t into a format like term?
double t = ((1.00 / f1) * ((((4.00 / (ak1 + 1.00)) - (2.00 / (ak1 + 4.00))) - (1.00 / (ak1 + 5.00))) - (1 / (ak1 + 6.00))));
To a format like
term = ((one.divide(f,mc)).multiply(((((four.divide((ak.add(one)),mc)).subtract((two.divide((ak.add(four)),mc)))).subtract((one.divide((ak.add(five)),mc)))).subtract((one.divide((ak.add(six)),mc))))));
I've tried recording it lots of times, and spent almost 6 hours trying to figure out where I'm getting wrong with the BigDecimal.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
不可以。不幸的是 Java 不支持运算符重载。您别无选择,只能在 BigDecimal 上使用这些方法。
No. Unfortunately Java does not support operator overloading. You have no choice but to use those methods on
BigDecimal
.没有。因为Java中没有运算符重载。
为了让事情变得更简单,将方程分成几部分写出来,并找出复杂或重复多次的部分。将计算分解为这些部分,甚至编写辅助方法来帮助您计算总和。如果您将整个计算分成几个部分,那么每个部分都更容易测试。
例如。
Nope. Since there is no operator overloading in Java.
To make things simpler for yourself write the equation out in parts and identity the bits that are complicated or repeated several times. Break up your computation into these parts and even write helper methods to help you get the sum computed. If you write the whole computation in parts then each part is easier to test.
eg.
您可以编写一个简单的解析器来构建表达式,然后使用 Rhino 或 Java 中可用的其他脚本引擎对其进行评估代码。
You could write a simple parser to build the expression then evaluate it using Rhino or other scripting engine available inside Java code.
您可以将表达式放在最后,而不是首先反转 f1。
您确定需要 BigDecimal 提供的精度吗?即您需要超过 15 位的精度。
prints
如果需要超过 15 位的精度,则需要 BigDecimal。但如果您想简化代码并且不需要那么高的精度,我会使用
double
。Instead of inverting f1 first, you could simply the expression by placing it last.
Are you sure you need the accuracy BigDecimal provides. i.e. you need more than 15 digits of precision.
prints
If you need more than 15 digits of accuracy, you need BigDecimal. But if you are trying to simplify your code and don't need so much precision, I would just use
double
.