#2834 Any better way to write math expression?

CretinHo Tue 15 Jun 2021

Current now, I have to write like this:

r := (a*a + b*b).sqrt

On Java, it make more sense to write as what you wrote in paper:

r = Math.sqrt(a*a + b*b)

So is there any better way to write math expression other than the above, e.g: like Java?

CretinHo Tue 15 Jun 2021

Oh, it's getting insane:

r := a.mult(a).plus(b.mult(b)).sqrt

SlimerDude Tue 15 Jun 2021

r := ((a*a) + (b*b)).sqrt - any better?

CretinHo Tue 15 Jun 2021

SlimerDude: still backward to what I normally write in Java but if it's the only style to write it on Fantom then I think I have to live with it! BTW, adding more redundancy brackets just make things more difficult to read. I still think my original is better:

r := (a*a + b*b).sqrt

SlimerDude Tue 15 Jun 2021

adding more redundancy brackets just make things more difficult to read.

Ah - I never did learn operator precedence, so I have to do it the long way!

still backward to what I normally write in Java

Maybe, but that's static methods vs instance methods for you! I personally liked the way Fantom moved everything to instance methods, it makes them easier to find!

go4 Wed 1 Sep 2021

Math function is extension method in fanx :

mixin Math {
  extensiosn static Float cos(Float self) { ... }
  extensiosn static Float sin(Float self) { ... }
}

class Main : Math {
  Void main() {
    f := 0.1

    //Fantom compatible style
    r := f.cos * f.sin

    //mathematics familiar style
    r := cos(f) * sin(f)
  }
}

Login or Signup to reply.