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.

Tyhm 01-27-2009 02:49 AM

Quote:

Originally Posted by LoneAngelIbesu (Post 1460299)
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.

I'd love to, but it's difficult to determine if the bitflag 262143 contains 32768 (2^15th) or not without doing a little division.

LoneAngelIbesu 01-27-2009 03:20 AM

Quote:

Originally Posted by Loriel (Post 1460333)
Nice theory, except booth floor and intval get 262143/32768 right.

In PHP, maybe. We're talking about GS2 here, so we have to assume that it's going to screw up easily with large decimals, as seen when it can't correctly subtract 8 from 7.999969482. Obviously, it's rounding 7.999969482 to 8, which is also why int(7.999969482) returns 8 instead of 7.

I did a quick test on RC. int(7.999) returns 7, but int(7.9999) returns 8. int(7.9998) returns 7, as well as int(7.999899999). Considering that 7.9999 and 7.999969482 have one thing in common, it's safe to assume that when Graal sees ".9999", it'll round up to the nearest whole number.

Quote:

Originally Posted by Tyhm (Post 1460358)
I'd love to, but it's difficult to determine if the bitflag 262143 contains 32768 (2^15th) or not without doing a little division.

What are you trying to accomplish, exactly? Perhaps if you showed us the problem in context, somebody might be able to figure something out (unless you're fine with using your work-around).

Skyld 01-27-2009 04:24 AM

Quote:

Originally Posted by Tyhm (Post 1460358)
I'd love to, but it's difficult to determine if the bitflag 262143 contains 32768 (2^15th) or not without doing a little division.

if ((262143 & 32768) != 0), no?

Tyhm 01-27-2009 06:16 AM

Huh. & might work, never tried it before;
The way I've got it set up Now is, it keeps an array of rooms that connect to other rooms; the array index is the room in question, the value at that index is a bitflag of which rooms it connects to (1 means 1, 2 means 2 and not 1, 3 means 2 and 1, 4 means 3, 5 means 3 and 1, etc). When it links two rooms it simply adds the values to combine the list of linked rooms, and then updates not just those 2 rooms but every room that links to those 2 rooms.

Moral of the story is, & might work for me, but it's not gonna make me happy; what, I have to revise every int(random(0,2)) now to have a special case for if it returns 2?!

LoneAngelIbesu 01-27-2009 11:10 PM

Quote:

Originally Posted by Skyld (Post 1460379)
if ((262143 & 32768) != 0), no?

I've never noticed that kind of syntax in GS2 (single ampersand)...

Chompy 01-27-2009 11:27 PM

Quote:

Originally Posted by LoneAngelIbesu (Post 1460609)
I've never noticed that kind of syntax in GS2 (single ampersand)...

It's very practical :)

PHP Code:

function onCreated() {
  
temp.bit 2+4+16;

  if (
bit 2) echo("2");
  if (
bit 4) echo("4");
  if (
bit 8) echo("8");
  if (
bit 16) echo("16");


Echoes

Quote:

2
4
16
(move() uses this)

Loriel 01-27-2009 11:49 PM

Pseudo-bitwise operation on what amounts to floating point numbers are a bit scary. :\

WhiteDragon 01-28-2009 06:49 AM

Interesting stuff on the whole rounding issue to anyone who is interested: http://wiki.php.net/rfc/rounding


All times are GMT +2. The time now is 11:43 PM.

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