#2907 Java's BigDecimal vs Fantom's Decimal

Gary Sun 11 Feb

How is it that the Java version using BigDecimal is so much more verbose than the Fantom version? They both ride on the JVM. Fantom's code appears to be more comparable to the C# version. I'm not complaining, it's a big plus for Fantom! :)

Java BigDecimal:

import java.math.BigDecimal;

public class Main {
    public static void main(String[] args) {
        BigDecimal decrement = BigDecimal.valueOf(0.1);
        BigDecimal x = BigDecimal.valueOf(1.0);
        BigDecimal zero = BigDecimal.valueOf(0.0);
        while (x.compareTo(zero) >= 0) {
            System.out.println(x);
            x = x.subtract(decrement);
        }
    }
}

Fantom Decimal:

class Main {
    static Void main() {
        for (n := 1.0d; n >= 0d; n -= 0.1d) {
            echo(n)
        }
    }
}

C# Decimal:

class decimaltype
{
    static void Main(string[] args)
    {
	for (decimal x = 1m; x >= 0.0m; x -= 0.1m) {
	    Console.WriteLine(x);
	}
    }
}

SlimerDude Wed 14 Feb

Yeah, it is pretty cool.

BigDecimal in Java was always a pain to work with - I usually ended writing my own wrapper class. But now in Fantom I don't have to! :)

Gary Fri 16 Feb

Fantom is a well-thought out language. I wonder if the developer brothers are twins by chance - they would be excellent at pair programming. My twin brother is an enterprise architect. In any case, I decided to make some of the variables constant which is always good coding practice of course. I wrote it in C# first because I'm a bit more familiar with the syntax. It's using top-level statements:

// C#:
using static System.Console;

var x = 1.0m;
WriteLine(x.GetType()); // System.Decimal
const decimal decrement = 0.1m;
const decimal zero = 0.0m;
    
while (x >= zero)
{
    WriteLine(x);
    x -= decrement;
}

Here's my attempt at translating it to Fantom. It's just a touch trickier than C# since const cannot be used with local variables:

// Fantom: 
using concurrent

class DecimalConst {

    static const Decimal decrement := 0.1d
    static const Decimal zero := 0.0d

    Void main() {
        x := 1.0d
        echo(x.typeof)

        while (x >= zero) {
        echo(x)    
        x -= decrement
        }
    }
}

Does my translation attempt appear to be idiomatic Fantom? It compiles and the output is as expected:

sys::Decimal
1.0
0.9
0.8
0.7
0.6
0.5
0.4
0.3
0.2
0.1
0.0

SlimerDude Fri 16 Feb

Hi Gary,

Yes, I would say that your code is very idiomatic Fantom - you've translated it well.

If you wanted to use a field value for x, that is also possible as shown below. (But following the mantra of functional programming that all state is evil, I would advise against it!)

using concurrent

class DecimalConst {

    static const Decimal decrement := 0.1d
    static const Decimal zero      := 0.0d
                 Decimal x         := 1.0d

    Void main() {
        echo(x.typeof)
        while (x >= zero) {
            echo(x)    
            x -= decrement
        }
    }
}

Note that Fantom does also support static and instance code blocks, but I would not advocate their use.

Login or Signup to reply.