General :  K-Meleon Web Browser Forum
General discussion about K-Meleon 
Strange behavior with integer comparison
Posted by: JohnHell
Date: April 10, 2009 06:09PM

My integers aren't comparing correctly.

I have this (1.1.x macro language style):

$varX <= 5 ? open($a) : $varX >= 11 ? open($b) : open($c);

It's clear the macro, right?

Well, imagine the var $varX have the value 7, or any other between 6 and 10. With these values, it should run the command open($c), but it never does. When the value is over 5, it runs, always the command open($b).

I also use the (+) sign before 5 and 11 and still fails :/

What I'm doing wrong? :-?

Options: ReplyQuote
Re: Strange behavior with integer comparison
Posted by: JamesD
Date: April 10, 2009 06:29PM

I think the 1.1.x macro language comparison is by character only. It does not do numeric comparsions. The 1.5.x version does do numeric comparisons. I will try to find some more info.

Options: ReplyQuote
Re: Strange behavior with integer comparison
Posted by: JamesD
Date: April 10, 2009 06:38PM

I found this from kko in a thread when I had same problem.
Quote

Posted by: kko
Date: December 20, 2008 08:14PM

Keep in mind that "<", ">", ... do perform string comparisons by default.

7 > 10 is true because the alphabetic order of 7 and 10 is: 10, 7

Try this:

$_Training1_Result_Bounds = +10 < $_Training1_Result ? 1 : "L";


(The first operand determines the type of the operation.)


Options: ReplyQuote
Re: Strange behavior with integer comparison
Posted by: JohnHell
Date: April 10, 2009 06:39PM

Ok, I'm going to search in my PC for the old macro language manual to have a look to it. I remember I downloaded that page... somewhere... xD I hope it is still here.


EDIT EDIT

Quote

(The first operand determines the type of the operation.)
ok.... so it should be inverted:


+5 >= $varX ? open($a) : +11 <= $varX ? open($b) : open($c);



Edited 1 time(s). Last edit at 04/10/2009 06:41PM by JohnHell.

Options: ReplyQuote
Re: Strange behavior with integer comparison
Posted by: JohnHell
Date: April 10, 2009 06:47PM

Still fails confused smiley

Options: ReplyQuote
Re: Strange behavior with integer comparison
Posted by: desga2
Date: April 10, 2009 07:28PM

Are you tried with brackets:

(+5 >= $varX) ? open($a) : (+11 <= $varX) ? open($b) : open($c);
or
(+5 >= $varX) ? open($a) : (+11 <= $varX ? open($b) : open($c));

K-Meleon in Spanish

Options: ReplyQuote
Re: Strange behavior with integer comparison
Posted by: JohnHell
Date: April 10, 2009 08:36PM

Tried an nothing desga :/

Disparity of results.

Options: ReplyQuote
Re: Strange behavior with integer comparison
Posted by: desga2
Date: April 10, 2009 10:35PM

What is the value of $varX?
$varX="3";
$varX=3;
$varX=+3;

K-Meleon in Spanish

Options: ReplyQuote
Re: Strange behavior with integer comparison
Posted by: JohnHell
Date: April 11, 2009 02:29AM

It's a result of the add up of 3 variables that take their values from the remainder (% operator) of the length of a given string and the current $URL.

To make it more graphical something as (with different var names):
$a = length($URL);
$b = length($thegivenstring);

$c = $a%3;
$d = $b%3;

$e = $c+$d; ($e is just $varX with other name (don't get lost winking smiley ))

In this process, isn't $e ever an integer? Indeed..., aren't all integers?

Note: at the beginning of this post I said 3, because I had in my test macro 3 remainders instead the 2 I wrote here.



Edited 1 time(s). Last edit at 04/11/2009 02:32AM by JohnHell.

Options: ReplyQuote
Re: Strange behavior with integer comparison
Posted by: JamesD
Date: April 11, 2009 09:49AM

@ JohnHell

Not sure how useful this will be, but I post it anyway. In the Training1.kmm file found in Training in the MacroLibrary http://kmeleon.sourceforge.net/wiki/KmmTraining is my workaround for the difference between 1.1.x and 1.5.x versions. Look at the "_Training1_PromptValidL{" and the "_Training1_PromptValidE{" macros.

I don't say this is the only way to handle the situation, it is just the way I did it.

This thread might also help. http://kmeleonbrowser.org/forum/read.php?1,86912,86939#msg-86939

Options: ReplyQuote
Re: Strange behavior with integer comparison
Posted by: JohnHell
Date: April 11, 2009 03:14PM

I'm lost now, not for that explanation JamesD, but for my test... Let's see:

calltesting{

$a = length($URL); # Note: my home url is about:blank, so from there is taking the value

$a == "about:blank" ? alert("text") : 0;  # obviously, here nothing to say
$a == 11 ? &asnumber : alert("is not a number"); # it's a number!
$a == "11" ? &astext : alert("is not text text"); # it's a text too!
}

asnumber{
alert("it is a number");
$a < 15 ? alert("less than 15") : $a > 15 ? alert("over 15") : alert("not less not over") ;
}
# if the previous function is used as is, the result is less than 15, ever, if I place (+) sign 
# before 15, it's ever over!! 15... ever!! 11 over 15!! So, how is the value treated then?
# And changing the order of the comparison to +15 > $a, for example, the result is the same


astext{
alert("it is text");
$a < 5 ? alert("less than 5") : $a > 5 ? alert("over 5") : alert("not less not over") ;
}

# here, it is ever less than 5


Options: ReplyQuote
Re: Strange behavior with integer comparison
Posted by: JamesD
Date: April 21, 2009 12:36AM

@ JohnHell

Sorry to be so long in getting an answer. Was on vacation and place we stayed did not have internet service.

I think the problem is that 1.1.x macro language compare is by character and not numeric value. I wrote a small bit of code to show what I mean.
_Snippet_RunCode{
##  $varX set to 12 returns _b
##  $varX set to 4 returns _a
##  $varX set to 6 returns _c
$varX = 6 ;
$varL = length($varX);
$varL < 2 ? $varX = "0" . $varX : 0 ;
alert("This is $varX  " . $varX, "DEBUG", INFO); 
#$varX <= 05 ? open($a) : $varX >= 11 ? open($b) : open($c);
$varX <= 05 ? &_a : $varX >= 11 ? &_b : &_c;

}
_c{alert("this is c");}
_b{alert("this is b");}
_a{alert("this is a");}

I just used alerts in place of open(). The sorted order of numbers works only until number of digits changes. The numbers 10, 11, and 12 do not sort correctly unless the numbers with a single digit are preceded with a zero.

1		01
10		02
11		03
12		04
2		05
3		06
4		07
5		08
6		09
7		10
8		11
9		12

I made 5 in your code into 05 and then checked to see if the $varX should have a zero preceding it. I think I got the correct answers doing it this way. I tested under KM ver 1.1.6

Options: ReplyQuote
Re: Strange behavior with integer comparison
Posted by: JohnHell
Date: April 21, 2009 03:10PM

Ok, I'll see, thanks for your testing, I took a rest after my last post and I didn't try to find a new solution.

I'll use that code to correct the digits problem. To be fair I was thinking on a bunch of conditionals xD but this, adapted, could be used for higher digits comparison (100, 1000, etc.). Two heads are better than one winking smiley

Options: ReplyQuote


K-Meleon forum is powered by Phorum.