#1206 Bitwise Operator on Int and Simple Calculations

Danielku15 Fri 10 Sep 2010

Hi Fantoms.

I'm currently evaluating Fantom because I want to move from Haxe to Fantom. I currently have two problems which I don't understand:

Bitwise Operators I've read that Bitwise operators are only supported by methods: No Problem. I tried this..

public Int index() 
{
  Int index := 0;
  Int val := this.value;

  while((val = (val.rshift(1))) > 0)
  {
    index++;
  } 
  return index;
} 

.. but i only get this error: Unknown method sys::Int.rshift Along the documentation (http://fantom.org/doc/sys/Int.html#shiftr) int supports this method. I'm using the Eclipse Plugin.

Simple Calculations Each application needs mathematical calulcations but it seems that simple statements like

Int time := (NoteDuration.QUARTER_TIME * (4.0/value)).toInt();

or

Int test := (10/3.0).toInt();

Are impossible: Invalid args div(sys::Decimal), not (sys::Int)

This sounds to me like a major bug in the type hierarchy.

Greetings Daniel

yachris Fri 10 Sep 2010

Hi Daniel,

The first one is simple... you're calling rshift, which does not exist; you want shiftr. Evidently you did a textual shift on your code :-)

The second one is not a bug in Fantom, but a case where it's being type-safe... I think. I'm not an expert in this area of Fantom, so if I'm wrong, I hope someone will correct my thinking.

The problem it is complaining about is the 10/3.0. In other languages, the 10 would be promoted to a float, the division done. In Fantom, you have to cause this to happen. So 10.toFloat() / 3.0f would work. Then they're both Floats, and the division can happen. Note the trailing f on the 3.0... they BOTH have to be Floats.

Danielku15 Fri 10 Sep 2010

Omg, how embarrassing. I'll update the shift code.

To the second type: True, It's an aspect of type-safety but in my eyes such a division should be possible without a lot of casting. I have a huge amount of complex calculations and it would be to bad if I need to transform each int into a float only for calculations. There should defenitly be a overload of those calculation methods to support Ints.

vkuzkokov Fri 10 Sep 2010

Consider 10.0 instead of 10. This way you will show that it's a real expression that just happened to have an integer value. If that's an integer variable (field, argument, return type), why not having an explicit conversion?

tactics Mon 13 Sep 2010

Also, note that in Fanton, semicolons are optional and that when calling a method of 0 parameters, the () are optional:

// valid
Int test := (10/3.0).toInt();

// preferred
Int test := (10/3.0).toInt

Login or Signup to reply.