Download MetaTrader 5
To add comments, please log in or register
Sell products via the Market and track your sales statistics
Mihai Ionescu
1288
Mihai Ionescu 2015.05.29 14:23 

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
2537
Drazen Penic 2015.05.29 15:20  

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
1288
Mihai Ionescu 2015.05.29 15:50  
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