Welcome to the Second Life Forums Archive

These forums are CLOSED. Please visit the new forums HERE

Vector conditional math error sometimes (compiler problem)

Strife Onizuka
Moonchild
Join date: 3 Mar 2004
Posts: 5,887
03-28-2004 01:14
when doing math on a conditional, as part of a vector constructor, a math error will be generated when the condition is false. But not when it is true.
example:

float a;
vector b=<1,(-1+2*(a>0)),1>;
when a>0
then b=<1,1,1>
when a<0
then a math error is thrown.

Workaround:
float a;
vector b=<1,(float)(-1+2*(a>0)),1>;

Request:
please add inline if statements :)
_____________________
Truth is a river that is always splitting up into arms that reunite. Islanded between the arms, the inhabitants argue for a lifetime as to which is the main river.
- Cyril Connolly

Without the political will to find common ground, the continual friction of tactic and counter tactic, only creates suspicion and hatred and vengeance, and perpetuates the cycle of violence.
- James Nachtwey
Tcoz Bach
Tyrell Victim
Join date: 10 Dec 2002
Posts: 973
03-30-2004 10:43
Is it a math error, or incompatible type?

I'd think -1+2*(a>0) would generate an integer, which is not a valid argument for a vector (float to int is implicit). That's why casting to float in your second example works. Your code would actually seem to show that everything is honky dory.
_____________________
** ...you want to do WHAT with that cube? **
Christopher Omega
Oxymoron
Join date: 28 Mar 2003
Posts: 1,828
03-30-2004 15:50
From: someone
Originally posted by Tcoz Bach
I'd think -1+2*(a>0) would generate an integer, which is not a valid argument for a vector (float to int is implicit). That's why casting to float in your second example works. Your code would actually seem to show that everything is honky dory.


From: someone
From the LSL documentation3.3.1.1. Implicit Casting
LSL only supports two implicit type casts: integer to float and string to key. Thus, any place you see a float specified you can supply an integer, and any place you see a key specified, you can supply a string.


Float -> int is explicit, your effectively loosing precision.

Int -> Float on the other hand, gains precision (well, if you disreguard floating point error for a few moments), so it was made implicit.

Implicit int -> float conversion lets us do:

float f = 1;
Without getting a syntax error. 1 is actually an integer.
If we didn't have implicit int -> float conversion, we'd have to explicitly put the decimal point in every float declaration.
ex:
float f = 1.0;

Implicit means that the developer has no need to cast one type to another. It is obvious why int -> float implicit was implemented, 1 can be an integer or a float, you dont want to have to type two more keys to add the ".0".

In LSL, its often best to seperate operators, sometimes the compiler gets confused and mistakes them for variable names.

Hope this helps.

EDIT:

Ooh, wait, this actually generates a runtime math error? How absurd.

Vector math is really messed up, its best if you just perform the operation outside the vector, then when your all finished, place everying, already evaluated, into the three vector positions.
Tcoz Bach
Tyrell Victim
Join date: 10 Dec 2002
Posts: 973
03-30-2004 19:31
My bad I was in a rush and saw the post. Int to float is implicit.

Chris, you as usual ENTIRELY overdid it. I stand by my post.
_____________________
** ...you want to do WHAT with that cube? **
Christopher Omega
Oxymoron
Join date: 28 Mar 2003
Posts: 1,828
03-30-2004 19:34
From: someone
Originally posted by Tcoz Bach
My bad I was in a rush and saw the post. Int to float is implicit.

Chris, you as usual ENTIRELY overdid it. I stand by my post.


I just didnt want the original poster, or anyone else reading this to get confused. I dont target you specificly Tcoz, honestly, I do things like that all the time.

Ow
==Chris
Cory Linden
Linden Lab Employee
Join date: 19 Nov 2002
Posts: 173
04-01-2004 16:42
Found and stomped. There was an error in the type pass on parenthetical expressions. Should be in the next full release we do early next week.