mIRC Home    About    Download    Register    News    Help

Print Thread
$calc error #195676 28/02/08 07:46 PM
Joined: Sep 2007
Posts: 6
N
Noot Offline OP
Nutrimatic drinks dispenser
OP Offline
Nutrimatic drinks dispenser
N
Joined: Sep 2007
Posts: 6
hi all, i see that $calc identifier have a bug.
When you write $calc(1/0), it returns 0 that is wrong.
1/0 is impossible. How do you resolve this problem?

Thx

Re: $calc error [Re: Noot] #195677 28/02/08 08:05 PM
Joined: Jan 2004
Posts: 509
L
LostShadow Offline
Fjord artisan
Offline
Fjord artisan
L
Joined: Jan 2004
Posts: 509
Originally Posted By: Noot
hi all, i see that $calc identifier have a bug.
When you write $calc(1/0), it returns 0 that is wrong.
1/0 is impossible.


As far as I know, mIRC doesn't use it's own built calculator, but the system's calculator. So this could be MicroSoft's fault, not Khaled's.

I guess you wanted it to return something like, something in English. Like "not possible." Or not simply return anything at all.

Quote:
How do you resolve this problem?

Thx


You can't change the default $calc() identifier. So you'll have to make your own custom $calculate() identifier or so.

Best of luck with that.

-Neal.

Re: $calc error [Re: LostShadow] #195679 28/02/08 08:21 PM
Joined: Oct 2003
Posts: 3,918
A
argv0 Offline
Hoopy frood
Offline
Hoopy frood
A
Joined: Oct 2003
Posts: 3,918
Originally Posted By: LostShadow

As far as I know, mIRC doesn't use it's own built calculator, but the system's calculator.


You probably don't know much then. Where do you even get your "facts" from? You could not be more wrong.

Hint: if you don't know an answer, don't reply. I know your OCD makes it hard for you not to type out every single thought you have, but please, for the sake of civility, stop.

edit:

PS. this isn't a bug. Sure, it's the wrong answer, but its your script logic that is supposed to be responsible for guarding against "impossible" results like those. Infinity cannot be represented numerically, so 0 is substituted, because it makes the most sense (of other possible values)-- 0 or $null. This is an intended behaviour. If mIRC had exception handling, it would raise one. mIRC has :error, but that's really overkill-- most people probably don't want control flow to change just because they divided by 0. For most rational results regarding the world of IRC-- N/0 is "0". $null wouldn't be bad, I suppose...

Last edited by argv0; 28/02/08 08:28 PM.

- argv[0] on EFnet #mIRC
- "Life is a pointer to an integer without a cast"
Re: $calc error [Re: argv0] #195680 28/02/08 08:25 PM
Joined: Jan 2004
Posts: 509
L
LostShadow Offline
Fjord artisan
Offline
Fjord artisan
L
Joined: Jan 2004
Posts: 509
Originally Posted By: argv0
Where do you even get your "facts" from?


Probably from what someone told me, and, for making the connection that mIRC uses the timestamp clock not from it's own clock, but the system's clock.

Quote:
I know your OCD


Close, autism.

-Neal.

Re: $calc error [Re: LostShadow] #195684 28/02/08 10:24 PM
Joined: Oct 2004
Posts: 8,330
Riamus2 Offline
Hoopy frood
Offline
Hoopy frood
Joined: Oct 2004
Posts: 8,330
Just about all software clocks use the system clock, so that's a moot point.

As for dividing by 0, like argv[0] said, just improve your script so it handles that possibility and either outputs an error message or else doesn't try to divide by 0 in the first place.


Invision Support
#Invision on irc.irchighway.net
Re: $calc error [Re: Noot] #195686 28/02/08 10:48 PM
Joined: Dec 2002
Posts: 2,962
S
starbucks_mafia Offline
Hoopy frood
Offline
Hoopy frood
S
Joined: Dec 2002
Posts: 2,962
Yes, it really should return $null to indicate an error IMO.


Spelling mistakes, grammatical errors, and stupid comments are intentional.
Re: $calc error [Re: starbucks_mafia] #195700 29/02/08 04:37 AM
Joined: Feb 2006
Posts: 546
J
jaytea Offline
Fjord artisan
Offline
Fjord artisan
J
Joined: Feb 2006
Posts: 546
i think so too, because detecting a div by 0 error would otherwise be too involved


"The only excuse for making a useless script is that one admires it intensely" - Oscar Wilde
Re: $calc error [Re: jaytea] #195701 29/02/08 05:15 AM
Joined: Oct 2004
Posts: 8,330
Riamus2 Offline
Hoopy frood
Offline
Hoopy frood
Joined: Oct 2004
Posts: 8,330
Too involved?

Code:
if (%divisor == 0) { echo -a Error: Divide by 0 | return }


Invision Support
#Invision on irc.irchighway.net
Re: $calc error [Re: Riamus2] #195703 29/02/08 05:25 AM
Joined: Feb 2006
Posts: 546
J
jaytea Offline
Fjord artisan
Offline
Fjord artisan
J
Joined: Feb 2006
Posts: 546
lolllllllllllll no

words fail me so i'll draw up an example

Code:
on *:text:!calc *:#:{ msg $chan the answer is: $calc($2-) }


now, show me how to easily detect if a division by 0 occurs


"The only excuse for making a useless script is that one admires it intensely" - Oscar Wilde
Re: $calc error [Re: jaytea] #195730 29/02/08 05:14 PM
Joined: Sep 2005
Posts: 2,881
H
hixxy Offline
Hoopy frood
Offline
Hoopy frood
H
Joined: Sep 2005
Posts: 2,881
Code:
alias -l isdivzero { return $regex($1,/\d[^+\-*/]*/[ ()]*0++(?=[^1-9]|$)/) }
alias calc2 { return $iif($isdivzero($1),$null,$calc($1)) }


//echo -a $calc2(1 * 3 / 3 ^ 2 / ((00)) * 2) -> $null
//echo -a $calc2(1 * 3 / 3 ^ 2 / ((00002)) * 2) -> 0.333333
//echo -a $calc2(1 * 3 / 3 ^ 2 / ((0)) * 2) -> $null
//echo -a $calc2(1 / 0) -> $null
//echo -a $calc2((1) / 0) -> $null

Re: $calc error [Re: hixxy] #195731 29/02/08 05:20 PM
Joined: Apr 2004
Posts: 847
Sat Offline
Hoopy frood
Offline
Hoopy frood
Joined: Apr 2004
Posts: 847
Quote:
!calc 1/(1-1)


Saturn, QuakeNet staff
Re: $calc error [Re: Sat] #195732 29/02/08 05:25 PM
Joined: Sep 2005
Posts: 2,881
H
hixxy Offline
Hoopy frood
Offline
Hoopy frood
H
Joined: Sep 2005
Posts: 2,881
Ouch frown

Re: $calc error [Re: Sat] #195733 29/02/08 05:53 PM
Joined: Sep 2005
Posts: 2,881
H
hixxy Offline
Hoopy frood
Offline
Hoopy frood
H
Joined: Sep 2005
Posts: 2,881
Code:
alias -l isdivzero { 
  if (!$regex($1,/\d[^+\-*\^%/]*/[ ()]*0++(?=[^1-9]|$)/)) {
    if ($lf isin $regsubex($remove($1,$lf),//[^+\-*\^%/]*(\d++[^+\-*\^%/]*(?:[+\-*\^%/][^+\-*\^%/]*\d++\51*)+)/,$iif(!$calc(\t),$lf))) { return 1 }
  }
  else { return 1 }
}
alias calc2 { return $iif($isdivzero($1),$null,$calc($1)) }



Re: $calc error [Re: hixxy] #195735 29/02/08 06:02 PM
Joined: Dec 2002
Posts: 2,962
S
starbucks_mafia Offline
Hoopy frood
Offline
Hoopy frood
S
Joined: Dec 2002
Posts: 2,962
That's nice, but I think we can all agree at this point that checking for divide-by-zero issues beforehand is non-trivial. In fact it requires you basically to implement your own $calc() just to verify the data before you pass it to the built-in one.


Spelling mistakes, grammatical errors, and stupid comments are intentional.
Re: $calc error [Re: LostShadow] #195736 29/02/08 06:15 PM
Joined: Jan 2004
Posts: 509
L
LostShadow Offline
Fjord artisan
Offline
Fjord artisan
L
Joined: Jan 2004
Posts: 509
It might be slippery slope to request that what about doing 1/3? And getting .333333.

Couldn't we change it to .3 with a bar on top? Or underlined?

So, 1 + 1/3 returns 1.3.

You just have to have mIRC design an algorithm to check for divisions that trigger infinite sig figs.

Or what about.

The square root of negative 1? Make it return i. Or something fancy.

Or what about e ^ (pi) (i) to return -1, rather than whatever it currently returns.

And so and so forth.

The list continues..

If there was a symbol for infinity, couldn't we use that for the 1 / 0?

-Neal.

Re: $calc error [Re: starbucks_mafia] #195737 29/02/08 06:21 PM
Joined: Sep 2005
Posts: 2,881
H
hixxy Offline
Hoopy frood
Offline
Hoopy frood
H
Joined: Sep 2005
Posts: 2,881
Yep. The first was meant to be a "simple" solution, the 2nd one I got a bit carried away with and forgot the purpose of making it in the first place!

Re: $calc error [Re: hixxy] #195746 29/02/08 10:00 PM
Joined: Oct 2003
Posts: 3,918
A
argv0 Offline
Hoopy frood
Offline
Hoopy frood
A
Joined: Oct 2003
Posts: 3,918
What, no recursive descent parser?


- argv[0] on EFnet #mIRC
- "Life is a pointer to an integer without a cast"
Re: $calc error [Re: argv0] #196266 14/03/08 12:16 AM
Joined: Feb 2003
Posts: 31
J
Jinx_Dojo Offline
Ameglian cow
Offline
Ameglian cow
J
Joined: Feb 2003
Posts: 31
$calc2(1 / (1 - 2 * 2 + 2 * 2)) = $null
$calc(1 / (1 - 2 * 2 + 2 * 2)) = 1

I appreciate the attempt, though, hixxy. It is much more productive than simply criticizing one's reasons for claiming the bug. I think, ideally, that checking for 0 divisors should be done alongside the calculation, since only at that point will there be an explicit value for the divisor. This could be reserved for a new, native calculate identifier, as well, so as to not destroy any backward compatibility with older scripts.

Re: $calc error [Re: Noot] #196267 14/03/08 12:31 AM
Joined: Feb 2006
Posts: 546
J
jaytea Offline
Fjord artisan
Offline
Fjord artisan
J
Joined: Feb 2006
Posts: 546
lol hixxy :*

$calc() also does things like evaluate variables/identifiers in cases such as $calc(1+$x), then there's $calc(0^-1) to worry about and such ;P

as starbucks said, detecting division by 0 involves almost exactly the same amount of work as re-creating $calc() using basic mathematical operations.. such as those offered in /set, which is no easy task! that's why i laughed at Riamus2's suggestion smile


"The only excuse for making a useless script is that one admires it intensely" - Oscar Wilde