Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Rounding numbers larger than 32k
#1
Rounding algorithms generally use the INT function which will not handle numbers greater than 32k.

CHALLENGE:
Write a subroutine, function or sub-program that can correctly round positive or negative numbers using the "conventional rounding method", which is: add .5 and truncate.

Input is a double precision number, which can be signed.
Output is also a double precision number,which is a whole number that can be signed.

Please test your entries.
*****
Reply
#2
Code:
n# = 100000000.9#
n# = INT(n# + .5)
print n#
The result in this example is 100000001, which is too large for a LONG. Proof positive that INT returns a double, thus fitting all the requirements for the challenge 8). Yes, I'm a lazy cheater :lol:. No doubt there's a more interesting way to do it.
Reply
#3
Sterling,

You're absolutely rignt. I tested it with up to 11 digit whole numbers with decimals, positive and negative, and it accepts them.

Where did I get the idea that INT could not handle numbers larger than the integer limit of 32k? I checked my manual, which is QuickBasic 4.0, although I'm running 4.5, and it says that it returns AN INTEGER. I even had underlined the word INTEGER. Maybe 4.0 had that restriction. Who knows?

In any event, SORRY GUYS FOR THE BOGUS CHALLENGE INFORMATION.
*****
Reply
#4
It does return an integer, but a mathematical integer. That is, a whole number.

Silly MS QB help.
Reply
#5
I finally discovered where I saw the limit of 32k, It's in the CINT function, not the INT. I never use CINT 'cause it gives some weird results for certain negative numbers.
*****
Reply


Forum Jump:


Users browsing this thread: 1 Guest(s)