Page 1 of 1

INT SURPRISES

Posted: Wed Jan 27, 2010 5:52 am
by LEGRAND
Can somebody explain how I can get the value I want:

h=44.10
D1# = INT(h * 100): PRINT D1#
D2 = 100 * h: PRINT D2

BASIC Answer for D1# is 4409 and not 4410
D2 answer is 4410
How can I have 4410 with INT fonction?
(h can be 44.1035 for instance. I need that INT to cut at that point the 2 last digits).
Thanks for help

Posted: Wed Jan 27, 2010 12:41 pm
by burger2227
Easiest way is to use CINT instead.

INT can never reach the multiplication number because it always rounds down. CINT, Closest Integer, rounds up or down.

Formulas for a range of numbers where max is highest number and min is lowest:

Code: Select all

INT formula: randNum = INT(RND * (max - min + 1)) + min 


CINT formula: randNum = CINT(RND * (max - min)) + min
AS you can see, CINT formula is simpler.

Ted

INT OR CINT

Posted: Wed Jan 27, 2010 2:09 pm
by LEGRAND
Great! fortunately, I can use CINT but it will limit the program to QBASIC.
In fact INT was commun to QBASIC and GWBASIC this is why I took it.
What about FIX ?

Posted: Wed Jan 27, 2010 3:35 pm
by burger2227
FIX only difference is with negative numbers. When negative it rounds less negative than INT which only rounds down as more negative.

INT always rounds down, FIX rounds positive values down too.

Code: Select all

PRINT INT(4.51)   ' = 4
PRINT FIX(4.51)   ' = 4
PRINT INT(-4.51)  ' = -5
PRINT FIX(-4.51)  ' = -4

INT SURPRISES

Posted: Wed Jan 27, 2010 4:54 pm
by LEGRAND
CINT Works well. Thanks again

Re: INT SURPRISES

Posted: Wed Jan 27, 2010 9:25 pm
by moneo
LEGRAND wrote:Can somebody explain how I can get the value I want:

h=44.10
D1# = INT(h * 100): PRINT D1#
D2 = 100 * h: PRINT D2

BASIC Answer for D1# is 4409 and not 4410
D2 answer is 4410
How can I have 4410 with INT fonction?
(h can be 44.1035 for instance. I need that INT to cut at that point the 2 last digits).
Thanks for help
Hi Legrand,
I spent about an hour testing your code. You're right, as it stands, you will get a erroneous result in D1# of 4409. Not all values cause this type of error. Using FIX instead of INT will give the same error.

The best I could determine is that this is a problem in the way BASIC evaluates expressions. I've had similar problems myself. To be truthful, I don't understand the problem.

However, if you perform temp = h * 100, and then put temp into the expression where the INT is, the problem goes away.

By the way, CINT works fine for your test case of 44.10. I didn't test other values. For values with more that 2 decimal digits, if you still multiply by 100, CINT may round it up using Bankers' Rounding.

Values having more than 2 decimal digits will require further testing.

Regards... Moneo

CINT new surprise

Posted: Fri Jan 29, 2010 9:50 am
by LEGRAND
Here is a new issue. The code is:
L = (CINT((36.5106 - CINT(36.5106)) * 100) / 60)
PRINT L
L2 = (CINT((CINT(36.5106) - 36.5106) * 100) / 60)
PRINT L2
The first one is negative ,The second positive.
Not really a good surprise. It is added to CINT(36.5106) whic gives 37
In other words I really don't know how to transform my angles from DD.MMSS to decimals: Depending of values results are correct (under **.5*** values (as 36.49.50) or wrong(as the one above)
I cannot believe there are not safe solutions.

Posted: Fri Jan 29, 2010 3:47 pm
by burger2227
CINT both values AFTER subtracting. The value returned = 0. This would indicate no difference in the two angles.

If you want better accuracy, don't use an INT or CINT integer return at all!