INT SURPRISES

If you have questions about any aspect of QBasic programming, or would like to help fellow programmers solve their problems, check out this board!

Moderators: Pete, Mods

Post Reply
LEGRAND
Coder
Posts: 49
Joined: Wed Jul 30, 2008 7:57 am

INT SURPRISES

Post by LEGRAND » Wed Jan 27, 2010 5:52 am

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

User avatar
burger2227
Veteran
Posts: 2344
Joined: Mon Aug 21, 2006 12:40 am
Location: Pittsburgh, PA

Post by burger2227 » Wed Jan 27, 2010 12:41 pm

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
Please acknowledge and thank members who answer your questions!
QB64 is a FREE QBasic compiler for WIN, MAC(OSX) and LINUX : https://www.qb64.org/forum/index.php
Get my Q-Basics demonstrator: https://www.dropbox.com/s/fdmgp91d6h8ps ... s.zip?dl=0

LEGRAND
Coder
Posts: 49
Joined: Wed Jul 30, 2008 7:57 am

INT OR CINT

Post by LEGRAND » Wed Jan 27, 2010 2:09 pm

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 ?

User avatar
burger2227
Veteran
Posts: 2344
Joined: Mon Aug 21, 2006 12:40 am
Location: Pittsburgh, PA

Post by burger2227 » Wed Jan 27, 2010 3:35 pm

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
Please acknowledge and thank members who answer your questions!
QB64 is a FREE QBasic compiler for WIN, MAC(OSX) and LINUX : https://www.qb64.org/forum/index.php
Get my Q-Basics demonstrator: https://www.dropbox.com/s/fdmgp91d6h8ps ... s.zip?dl=0

LEGRAND
Coder
Posts: 49
Joined: Wed Jul 30, 2008 7:57 am

INT SURPRISES

Post by LEGRAND » Wed Jan 27, 2010 4:54 pm

CINT Works well. Thanks again

moneo
Veteran
Posts: 451
Joined: Tue Jun 28, 2005 7:00 pm
Location: Mexico City, Mexico

Re: INT SURPRISES

Post by moneo » Wed Jan 27, 2010 9:25 pm

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

LEGRAND
Coder
Posts: 49
Joined: Wed Jul 30, 2008 7:57 am

CINT new surprise

Post by LEGRAND » Fri Jan 29, 2010 9:50 am

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.

User avatar
burger2227
Veteran
Posts: 2344
Joined: Mon Aug 21, 2006 12:40 am
Location: Pittsburgh, PA

Post by burger2227 » Fri Jan 29, 2010 3:47 pm

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!
Please acknowledge and thank members who answer your questions!
QB64 is a FREE QBasic compiler for WIN, MAC(OSX) and LINUX : https://www.qb64.org/forum/index.php
Get my Q-Basics demonstrator: https://www.dropbox.com/s/fdmgp91d6h8ps ... s.zip?dl=0

Post Reply