Graal Forums

Graal Forums (https://forums.graalonline.com/forums/index.php)
-   Bug Report (https://forums.graalonline.com/forums/forumdisplay.php?f=193)
-   -   int is useless (https://forums.graalonline.com/forums/showthread.php?t=83882)

Tyhm 01-26-2009 01:15 AM

int is useless
 
int (-1.5) returns -2, not -1.
int(262143/32768) (which is 7.999969482421875) returns 8, not 7.

This is, understandably, brutally murdering all my attempts to script.

Tyhm 01-26-2009 01:34 AM

In case anyone else is having this problem:

function trueInt(){
setstring DammitStefan,#v(intThis);
intThis=strtofloat(#e(0,indexof(.,#s(DammitStefan) ),#s(DammitStefan)));
}

xXziroXx 01-26-2009 05:07 PM

Quote:

Originally Posted by Tyhm (Post 1459991)
int (-1.5) returns -2, not -1.

This is, understandably, brutally murdering all my attempts to script.

int() rounds decimals down, and thus -1.5 becomes -2 in the same way 1.5 would become 1. If it bothers you that much, you can always abs() it prior do the int().

Loriel 01-26-2009 05:31 PM

Quote:

Originally Posted by xXziroXx (Post 1460172)
Quote:

Originally Posted by Tyhm
int(262143/32768) (which is 7.999969482421875) returns 8, not 7.

int() rounds decimals down

:\

xXziroXx 01-26-2009 06:10 PM

Quote:

Originally Posted by Loriel (Post 1460179)
:\

Right.

Deas_Voice 01-26-2009 06:11 PM

Quote:

Originally Posted by xXziroXx (Post 1460172)
int() rounds decimals down, and thus -1.5 becomes -2 in the same way 1.5 would become 1. If it bothers you that much, you can always abs() it prior do the int().

returns 7.999969482

xXziroXx 01-26-2009 06:12 PM

Quote:

Originally Posted by Deas_Voice (Post 1460189)
returns 7.999969482

I never replied to the 7.99adjhafgafe whatever, now did I?

Deas_Voice 01-26-2009 06:14 PM

Quote:

Originally Posted by xXziroXx (Post 1460191)
I never replied to the 7.99adjhafgafe whatever, now did I?

uhm.....
no?
so what? :p

Loriel 01-26-2009 07:48 PM

Whatever, int()'s behaviour is obviously inconsistent, it does not work like C's int casts or floor(), and if there is any documentation that explains its behaviour I must have missed it in the past couple of years.

Unless you can explain how to use int() to get the effect that Tyhm is achieving with his horrible string manipulation abomination, stop trolling.

LoneAngelIbesu 01-26-2009 11:00 PM

From my experiences with PHP, when using fractions, you might lose float point precision because it's impossible to express some fractions in a finite number of digits. This commonly affects the use of intVal(), which is the PHP equivalent to GS2's int().

From PHP.net website:
Quote:

Warning
Floating point precision

It is typical that simple decimal fractions like 0.1 or 0.7 cannot be converted into their internal binary counterparts without a small loss of precision. This can lead to confusing results: for example, floor((0.1+0.7)*10) will usually return 7 instead of the expected 8, since the internal representation will be something like 7.9.

This is due to the fact that it is impossible to express some fractions in decimal notation with a finite number of digits. For instance, 1/3 in decimal form becomes 0.3 (repeating).

So never trust floating number results to the last digit, and never compare floating point numbers for equality. If higher precision is necessary, the arbitrary precision math functions and gmp functions are available.

Note: floor() is a PHP function that rounds a number down to the whole number closest to zero... which is probably closer to GS2's int() than intVal(), since intVal() accepts a base (defaults to base 10) parameter.

Tyhm 01-26-2009 11:29 PM

Bee Ess. Any language that thinks 0.1+0.7=0.79 is not a language worth trusting.

LoneAngelIbesu 01-26-2009 11:35 PM

Quote:

Originally Posted by Tyhm (Post 1460295)
Bee Ess. Any language that thinks 0.1+0.7=0.79 is not a language worth trusting.

It's probably more complex than you think it is. PHP is a very good language. The point I was trying to make, though, was that you should try and not use non-terminating decimals, or at least round them to terminating decimals.

Using large decimals is bound to cause problems. For instance, in GS2 "7.999969482 - 8" produces 0. But, "7.99 - 8" produces -0.01. So, I wouldn't say that int() is useless. It just faces the same problems that other languages face when it comes to floating points.

WhiteDragon 01-27-2009 12:45 AM

I don't think it should round down, it should round towards zero.

The floating point issue has already been covered above.

I agree there should be a "floor" in Graal Script like there is in PHP, or the behavior of int should be changed.

Loriel 01-27-2009 01:40 AM

Quote:

Originally Posted by LoneAngelIbesu (Post 1460291)
From my experiences with PHP, when using fractions, you might lose float point precision because it's impossible to express some fractions in a finite number of digits. This commonly affects the use of intVal(), which is the PHP equivalent to GS2's int().

From PHP.net website:


Note: floor() is a PHP function that rounds a number down to the whole number closest to zero... which is probably closer to GS2's int() than intVal(), since intVal() accepts a base (defaults to base 10) parameter.

Nice theory, except booth floor and intval get 262143/32768 right.

DarkReaper0 01-27-2009 02:24 AM

Quote:

Originally Posted by xXziroXx (Post 1460191)
I never replied to the 7.99adjhafgafe whatever, now did I?

No, but if you paid attention you'd realize not recognizing that point 'brutally murdered' your response.


All times are GMT +2. The time now is 10:54 AM.

Powered by vBulletin® Version 3.8.11
Copyright ©2000 - 2026, vBulletin Solutions Inc.
Copyright (C) 1998-2019 Toonslab All Rights Reserved.