Download MetaTrader 5

why is int() different than MathFloor() for negative numbers?

To add comments, please log in or register
Mihai Ionescu
1485
Mihai Ionescu  

I mean

 int(-2.5) -2

MathFloor(-2.5) = -3


why is this? 

/* 

I stumbled upon this issue when trying to create a function getlastmonday(datetime dt)

I expected -1 % 7 = 6

but it gives -1

*/ 

Drazen Penic
2678
Drazen Penic  

From the help on MathFloor():  "A numeric value representing the largest integer that is less than or equal to val."

First integer less than -2.5 is -3

 

From the help on Typecasting:  "As a result of converting floating point values to integer type, the fractional part is always deleted. If you want to round off a float to the nearest whole number (which in many cases is more useful), you should use MathRound(). "

Casting simply cuts off decimals - no rounding => -2.5 without decimal part gives -2. 

Mihai Ionescu
1485
Mihai Ionescu  
Drazen Penic:

From the help on MathFloor():  "A numeric value representing the largest integer that is less than or equal to val."

First integer less than -2.5 is -3

 

From the help on Typecasting:  "As a result of converting floating point values to integer type, the fractional part is always deleted. If you want to round off a float to the nearest whole number (which in many cases is more useful), you should use MathRound(). "

Casting simply cuts off decimals - no rounding => -2.5 without decimal part gives -2. 

Thank you

It's clear now. For so many years I thought int() is the same as floor()

I verified now for python also, it behaves the same. 

To add comments, please log in or register