Convert to Integer function - page 2

 

This looks to me like a discussion whether keep the type of the variable the same or to change the type from double (floating point rep) to integer. To make that kind of decision we really ought to know the purpose of the variable and how it is going to be used and any other considerations like speed and memory.

 
1005phillip:

Fortunately it is easily testable. Run the attached script, investigate the math. Works for me. MQL seems to deftly manage distinguishing between true integers and "equivalent" integers as you put it and mathfloor, mathceil, and mathround all perform as expected.

try
Print( MathFloor(1.1)/2 ); // prints 0.5
Print( Int(1.1)/2 );       // prints 0
 

That condition is already covered in the referenced script.

Print( MathFloor(MathFloor(1.1)/2) ); // prints 0
Print( Int(1.1)/2 );       // prints 0
 

Not tried, but reading this brought up the question in my mind of trying

Print( DoubleToStr(MathFloor(MathFloor(1.1)/2) ),2); // prints ?
Print( DoubleToSTr(Int(1.1)/2 ),2);       // prints ?

Otherwise your assuming that the Print function does not do its own type casting to integer.

 
Ickyrus:

Not tried, but reading this brought up the question in my mind of trying

Otherwise your assuming that the Print function does not do its own type casting to integer.


I'm not making that assumption, I use the boolean logic in the script to ensure the result is truly an integer, but the comments that have been made above are expanding on that.
 

One option I have not seen in converting to and integer is

Double x ;
x=ask ;
Print(x-MathMod(x,1)) ;

of course floating point arithmatic has errors and then there is the question of if you want to round up or round down.

Reason: