Page 1 of 1 1
Topic Options
#205415 - 2012-06-24 01:20 PM Negative Numbers (Bug?)
paperinik Offline
Fresh Scripter

Registered: 2007-11-04
Posts: 23
Loc: Germany
Hello,

Whats wrong with negative numbers?

This Script:
---------------
gets $b

if $b >"-14"
? "blabla"
endif

-----------------
OR this script:
----------------
gets $b

if $b >-14
? "blabla"
endif

----------------

Problem:
-17 kix write "blabla" (wrong behavior)
-10 kix write nothing (wrong behavior)
-9 kix write "blabla" (right behavior)

kixversion tested: 4.62 4.53

Top
#205416 - 2012-06-24 02:04 PM Re: Negative Numbers (Bug?) [Re: paperinik]
Allen Administrator Offline
KiX Supporter
*****

Registered: 2003-04-19
Posts: 4562
Loc: USA
Your first example with the quotes will not work the way you expect because you are comparing strings rather than numbers.

Check out val() and vartypename() in the manual. And apply this to $b.


Top
#205417 - 2012-06-24 03:52 PM Re: Negative Numbers (Bug?) [Re: Allen]
paperinik Offline
Fresh Scripter

Registered: 2007-11-04
Posts: 23
Loc: Germany

Have you tried example1 or example2?

If you think example1 is not good, try only example2.

Type in the numbers, I gave as examples.
What are your results?

If you think the script has to be changed to compare two (negative) numbers (e.g. > or < -n), please write it.

Top
#205418 - 2012-06-24 04:41 PM Re: Negative Numbers (Bug?) [Re: paperinik]
Lonkero Administrator Offline
KiX Master Guru
*****

Registered: 2001-06-05
Posts: 22346
Loc: OK
get and gets are string input just like readline. Kixtart converts the right side to the left side type to do the comparison. So try:
If -14<$b

It works just as expected and as far as I remember this is explained in the manual.
_________________________
!

download KiXnet

Top
#205420 - 2012-06-24 06:41 PM Re: Negative Numbers (Bug?) [Re: Lonkero]
paperinik Offline
Fresh Scripter

Registered: 2007-11-04
Posts: 23
Loc: Germany
@lonkero:

Thanks. It works.
I search in the manual, but I didnīt found these hints there.

Top
#205421 - 2012-06-24 07:14 PM Re: Negative Numbers (Bug?) [Re: paperinik]
Allen Administrator Offline
KiX Supporter
*****

Registered: 2003-04-19
Posts: 4562
Loc: USA
 Code:
gets $b
? vartypename($b)

if val($b)>-14
? "blabla"
endif


Edited by Allen (2012-06-24 09:48 PM)

Top
#205422 - 2012-06-24 07:51 PM Re: Negative Numbers (Bug?) [Re: Allen]
paperinik Offline
Fresh Scripter

Registered: 2007-11-04
Posts: 23
Loc: Germany
@Allen:
your example didnīt work.
The script only wrote the vartype. Then it ends.

The solution of @Lonkero is the right one:
--------------
gets $b

if -14<$b
? "blabla"
endif
------------

Then all works.

Top
#205423 - 2012-06-24 09:48 PM Re: Negative Numbers (Bug?) [Re: paperinik]
Allen Administrator Offline
KiX Supporter
*****

Registered: 2003-04-19
Posts: 4562
Loc: USA
There was a $ in front of val by accident. try it again.
Top
#205427 - 2012-06-26 06:57 AM Re: Negative Numbers (Bug?) [Re: Allen]
paperinik Offline
Fresh Scripter

Registered: 2007-11-04
Posts: 23
Loc: Germany
@Allen: now your script work too.

So there are two ways to solve the problem.

Thanks to @Allen and thanks to @Lonkero.

Top
#205428 - 2012-06-26 07:59 PM Re: Negative Numbers (Bug?) [Re: paperinik]
ShaneEP Moderator Offline
MM club member
*****

Registered: 2002-11-29
Posts: 2127
Loc: Tulsa, OK
It's not really a problem, as much as it is a misunderstanding of how the scripting language works. You can't expect to compare a string variable with an integer variable and get results you want.

When you use Get or Gets it returns a string value, and you were trying to compare it to an integer.

When Kix sees a line like "123" > 123 it assumes that you are comparing two strings since the variable on the left side of the > is a string...So it treats the right side of the > as a string as well. When you reverse the order 123 < "123" then Kix assumes that two integers are being compared since the variable on the left is an integer...so it then treats the right side as an integer as well.

Like Allen pointed out, you can also force a variable to be converted to a specific type by using a number of built-in functions...CInt(), CStr(), CDbl(), Val(), etc...

Top
#205429 - 2012-06-26 08:19 PM Re: Negative Numbers (Bug?) [Re: ShaneEP]
Les Offline
KiX Master
*****

Registered: 2001-06-11
Posts: 12734
Loc: fortfrances.on.ca
 Originally Posted By: ShaneEP
It's not really a problem...
PEBKAC!
_________________________
Give a man a fish and he will be back for more. Slap him with a fish and he will go away forever.

Top
Page 1 of 1 1


Moderator:  Jochen, Allen, Radimus, Glenn Barnas, ShaneEP, Ruud van Velsen, Arend_, Mart 
Hop to:
Shout Box

Who's Online
0 registered and 657 anonymous users online.
Newest Members
M_Moore, BeeEm, min_seow, Audio, Hoschi
17883 Registered Users

Generated in 0.068 seconds in which 0.03 seconds were spent on a total of 13 queries. Zlib compression enabled.

Search the board with:
superb Board Search
or try with google:
Google
Web kixtart.org