WinBatch® Technical Support Forum

All Things WinBatch => WinBatch => Topic started by: snowsnowsnow on June 18, 2019, 11:08:56 PM

Title: The Int() function is really Round()
Post by: snowsnowsnow on June 18, 2019, 11:08:56 PM
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.
Title: Re: The Int() function is really Round()
Post by: kdmoyers on June 19, 2019, 05:17:35 AM
I guess that *many* programs, old and new, knowingly or unknowlingly, rely on that behavior. 
Title: Re: The Int() function is really Round()
Post by: td on June 19, 2019, 07:06:42 AM
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.
Title: Re: The Int() function is really Round()
Post by: snowsnowsnow on June 19, 2019, 04:01:49 PM
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).
Title: Re: The Int() function is really Round()
Post by: 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.
Title: Re: The Int() function is really Round()
Post by: snowsnowsnow on June 20, 2019, 03:12:14 AM
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.
Title: Re: The Int() function is really Round()
Post by: 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?
Title: Re: The Int() function is really Round()
Post by: td on June 20, 2019, 09:18:52 AM
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." 
Title: Re: The Int() function is really Round()
Post by: stanl on June 20, 2019, 09:40:41 AM
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
Title: Re: The Int() function is really Round()
Post by: snowsnowsnow on June 20, 2019, 01:45:57 PM
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.