The Int() function is really Round()

Started by snowsnowsnow, June 18, 2019, 11:08:56 PM

Previous topic - Next topic

snowsnowsnow

Note: I think this may have been discussed previously, but I don't remember what the outcome of that discussion was.

Anyway, I've noticed that Int() doesn't work like Int() in most other languages.  That is, it rounds, rather than truncates.  I hit this when I was trying to do some calculations on filesizes.  I wanted to take a filesize and divide it by 8 million, truncating.

But I found that Int(FileSize(fn)/8e6) returns 7 for a filesize of 52,000,000.

I would like it to return 6 for anything less than 56,000,000.

The workaround that I came up with is to write:

Int((FileSize(fn)-4e6)/8e6)

but I am, of course, wondering if there is a better way.

kdmoyers

I guess that *many* programs, old and new, knowingly or unknowlingly, rely on that behavior. 
The mind is everything; What you think, you become.

td

Quote from: snowsnowsnow on June 18, 2019, 11:08:56 PM
but I am, of course, wondering if there is a better way.

I am not aware of any standard that dictates how programming languages' "Int" function must be implemented and the WIL Int function does round floating point numbers to the nearest integer  If you want a floating point number's floor then you might want to consider using the WIL "Floor" function.
"No one who sees a peregrine falcon fly can ever forget the beauty and thrill of that flight."
  - Dr. Tom Cade

snowsnowsnow

I certainly didn't mean to imply that it was a bug or any kind of defect.  Just that it is unexpected.  Yes, I'm aware that each programming language is free to define terms independently of any/all other languages, but the fact is that there are expectations about what things do.

Anyway, of course I'm not asking for it to be changed, since that would (obviously) break existing code.  I was asking if there was another way to do it, and Floor() looks like it could very well be the answer (at least if one restricts oneself to positive numbers).

td

Didn't mean to sound "snarky".  It has been a long day.  The floor function should handle negative floating point numbers.  For example, -1.4 will return -2.
"No one who sees a peregrine falcon fly can ever forget the beauty and thrill of that flight."
  - Dr. Tom Cade

snowsnowsnow

Quote from: td on June 19, 2019, 05:48:19 PM
Didn't mean to sound "snarky".  It has been a long day.  The floor function should handle negative floating point numbers.  For example, -1.4 will return -2.

Right, but that's not what Int() (in my perfect world) should do.

Int() (in other languages) truncates towards zero.

So, that's why I said that Floor() only works like (my ideal version of) Int() - for positive numbers.

Anyway, it looks like the problem is solved.  I'll just use Floor() in future.

ChuckC

sounds like a case for a UDF.... if the floating point # "x" is less than zero, just use Floor(-x); otherwise, use Floor(x).

or has my coffee not kicked in, yet, this morning?

td

Interesting.  The Excel "Int" function always rounds down and not toward zero.  To quote from MSFT's documentation, "... so negative numbers become more negative. INT( 10.8 ) returns 10, INT( -10.8 ) returns -11." 
"No one who sees a peregrine falcon fly can ever forget the beauty and thrill of that flight."
  - Dr. Tom Cade

stanl

Quote from: ChuckC on June 20, 2019, 05:50:01 AM
sounds like a case for a UDF.... if the floating point # "x" is less than zero, just use Floor(-x); otherwise, use Floor(x).

or has my coffee not kicked in, yet, this morning?


or maybe
Code (WINBATCH) Select

x=-12.31
If x<0
   x=Ceiling(x)
Else
   x=Floor(x)
Endif

snowsnowsnow

Yep, Stan.  That's looks right.

Anyway, I ended up solving the problem another way, so am not using the "divide by 8 million" method anymore.