The precision of the built-in number type Double is just not precise enough for me. Users might want to define rounding policies and precisions. What can I do in Appway to achieve this?
This recipe outlines the basics and provides some ideas on how to enhance the feature even further. If you don't believe that Double type can be the wrong choice, I have a small example that clarifies the issue.
Consider that BigDecimal is currently considered an
unrecommended Primitive Type and is therefore not available in the Business Object selector. If necessary, you can still add it to your
whitelist.
Appway uses the Java Double type to do its calculations. All calculations are restricted to the IEEE 754 standard. As such, the numbers that can be respresented by the binary fraction are somehow limited.
Let's take a look at the following script.
Integer $i := 0;
Double $val := 0.0;
For $i := 0 Condition $i < 10 Step $i := $i + 1 Do
$val := $val + 0.1;
PRINTLN($val);
End
The expected output is this:
0.1
0.2
0.3
0.4
...
1.0
Real output is:
0.1
0.2
0.30000000000000004
0.4
0.5
0.6
0.7
0.7999999999999999
0.8999999999999999
0.9999999999999999
Hmm, how can any result be accurate if the simple decimal add operation shows such obvious errors?
Well, the problem lies in the IEEE 754 standard used for Double type — which, by the way, is used in almost all programming languages.
(IEEE 754 details)
If we use BigDecimal instead, we get the following:
Integer $i := 0;
BigDecimal $val := NewObject(BigDecimal, '0.0');
For $i := 0 Condition $i < 10 Step $i := $i + 1 Do
$val := $val.add(NewObject(BigDecimal, '0.1'));
PRINTLN($val);
End
...
0.1
0.2
0.3
0.4
0.5
0.6
0.7
0.8
0.9
1.0
So, read the following sections to find out how to install BigDecimal and how to use it.
- Add a primitive type for BigDecimals to Appway type library.
Type BigDecimal Begin
JavaType java.math.BigDecimal;
MetaData['AutoConstruct', 'true'];
End
- When constructing this type you ALWAYS have to give it a initial value. Use the function
NewObject to construct a BigDecimal.
BigDecimal $bd1 := NewObject(BigDecimal, '0.25')
The standard math functions work with BigDecimals, but a conversion to type "double" is made. So, these functions should not be used if you want to stay in the BigDecimal universe. Instead, use the methods that are given by the BigDecimal type:
Addition
BigDecimal $bd1 := NewObject(BigDecimal, '0.25');
BigDecimal $bd2 := NewObject(BigDecimal, '0.25');
$bd1.add($bd2);
Subtraction
BigDecimal $bd1 := NewObject(BigDecimal, '0.25');
BigDecimal $bd2 := NewObject(BigDecimal, '0.25');
$bd1.subtract($bd2);
Multiplication
BigDecimal $bd1 := NewObject(BigDecimal, '0.25');
BigDecimal $bd2 := NewObject(BigDecimal, '0.25');
$bd1.multiply($bd2);
Division
Division is a bit tricky because you have to define the precision and the rounding policy.
BigDecimal $bd1 := NewObject(BigDecimal, '0.25');
BigDecimal $bd2 := NewObject(BigDecimal, '0.25');
// round 2 digits after the semicolon
// use policy ROUND_DOWN
$bd1.divide($bd2, 2, 1);
That was easy. Of course we cannot expose this strange division method with three parameters to the user. Instead, we can hide the complexity by implementing some easier-to-use functions. Read the next section to discover what this means.
BigDecimal alone is a nice thing. Using these you make sure that you always have the most precise value you can get. The new type is even more useful when used in a class for Money support.
DataClass Money Begin
Property[amount, BigDecimal, None];
Property[currency, String, None];
Function NEW() : Nothing Begin
// initialize amount to 0, currency to CHF
$this.amount := NewObject(BigDecimal, '0.0');
$this.currency := 'CHF';
End
Function divideBy(Double $d) : BigDecimal Begin
BigDecimal $bd := $this.amount.divide(NewObject(BigDecimal, TOSTRING($d)), 2, 1);
Return $bd;
End
End
The function Money.divideBy hides the complexity of the BigDecimal division method. In this function we can of course apply the rounding policies of the customer. For example use a User preference which defines the precision and rounding strategy with a fallback to content or server configuration.
You can add whatever function you find useful to your Money class. For example converting into different currencies, adding, subtracting, division etc.
Here's the output when using "Double":
Integer $i := 0;
Double $val := 0.0;
For $i := 0 Condition $i < 1000000 Step $i := $i + 1 Do
$val := $val + 0.1;
End
execution took 44.147 s
For comparison, here's the ouput when using BigDecimal:
Integer $i := 0;
BigDecimal $val := NewObject(BigDecimal, '0.0');
For $i := 0 Condition $i < 1000000 Step $i := $i + 1 Do
$val := $val.add(NewObject(BigDecimal, '0.1'));
End
execution took 80.575 s
If the incrementation value is cached, we save about a third due to object construction time that was saved.
Integer $i := 0;
BigDecimal $val := NewObject(BigDecimal, '0.0');
BigDecimal $inc := NewObject(BigDecimal, '0.1');
For $i := 0 Condition $i < 1000000 Step $i := $i + 1 Do
$val := $val.add($inc);
End
execution took 61.989 s
BigDecimal calculation has a performance penalty of factor 1.5 compared to Double calculation.
- Use BigDecimal if exact results are required.
- Use Double if execution speed is required.
As a rule of thumb I'd say that whenever you have to deal with some money amount you should actually use BigDecimal and accept the performance penalty.
Java 1.6.25 introduced an experimental performance improvement to the BigDecimal class, enhancing its performance by 30 percent. BigDecimal optimization is enabled by specifying -XX:+AggressiveOpts command option in the Java VM start command.