Math-ish Question for Programmers
|
|
Julian Fate
80's Pop Star
Join date: 19 Oct 2003
Posts: 1,020
|
03-17-2006 17:21
What is the name of the operation that reduces the number of decimal places displayed in a number? Like, someFunction(0.2304923094, 4) would return 0.2304. I'm looking for what it's called when you do that. It isn't rounding or finding the floor but it has a name like those.
Secondly, does LSL have a native function to do that or do I need to supply my own?
Thanks!
|
|
Alondria LeFay
Registered User
Join date: 2 May 2003
Posts: 725
|
03-17-2006 17:23
I think the name would vary by language (assuming they have a built in function even) - it basically would be setting the precision of the decimal.
There isn't a llFunction that does this directly in LSL, although it should not be too problematic to create a function to.
|
|
Julian Fate
80's Pop Star
Join date: 19 Oct 2003
Posts: 1,020
|
03-17-2006 17:36
Thank you very much. Precision is the word I was looking for.
|
|
Cid Jacobs
Theoretical Meteorologist
Join date: 18 Jul 2004
Posts: 4,304
|
03-17-2006 17:44
Cast to a string, llGetSubString, cast back to a float.
|
|
Ordinal Malaprop
really very ordinary
Join date: 9 Sep 2005
Posts: 4,607
|
03-17-2006 18:21
If you're turning 0.2304923094 into 0.2304, you're really talking about truncating the number, or a string representing the number. Normally, if you were changing the precision to 4 d.p. you'd end up with 0.2305, since you round up the figure after the last one.
|
|
Julian Fate
80's Pop Star
Join date: 19 Oct 2003
Posts: 1,020
|
03-17-2006 18:27
If anyone is interested, here's what I threw together to prettify a vector for display. Let me know if it gives you problems or is written stupidly. string getPreciseVector(vector V, integer precision) { string VX = (string)V.x; string VY = (string)V.y; string VZ = (string)V.z; VX = llGetSubString(VX,0,precision); VY = llGetSubString(VY,0,precision); VZ = llGetSubString(VZ,0,precision); string VNew = "<"+VX+","+VY+","+VZ+">"; return VNew; }// getPreciseVector()I'm not returning a vector and displaying "  string)getPreciseVector(<1,1,1>,3)" because casting it back to a vector loses the precision.  It's really only useful for text formatting.
|
|
Static Sprocket
Registered User
Join date: 10 Feb 2006
Posts: 157
|
03-17-2006 21:14
From: someone Like, someFunction(0.2304923094, 4) would return 0.2304 Personally I don't care for converting to and back from strings. Something like this will generally work for you: float roundTo(float x, integer precision) { x = x * (llPow(10,precision)); // move everything we want to keep to the left of the decimal
x = llRound(x); // Round it
x = x / (llPow(10,precision)); // shift everything back to where we had it.
return x; }
|
|
Hugsy Penguin
Sky Junkie
Join date: 20 Jun 2005
Posts: 851
|
03-17-2006 21:50
This variation of Static's code is useful for display as it chops off trailing zeros (and properly rounds, not truncates). string MakeStr(float x, integer precision) { x = x * (llPow(10,precision)); // move everything we want to keep to the left of the decimal x = llRound(x); // Round it x = x / (llPow(10,precision)); // shift everything back to where we had it.
string s = (string)x; return llGetSubString(s, 0, llSubStringIndex(s, ".") + precision); }
default { touch_start(integer total_number) { llOwnerSay(MakeStr(PI, 3)); } }
HTH, HP
|
|
Strife Onizuka
Moonchild
Join date: 3 Mar 2004
Posts: 5,887
|
03-17-2006 23:28
Functions for safely transporting floats (though strings) without having to have a special parser or corruption. Float2HexFloat2SciConverts a float to it's memory representation (as an integer) and back again, no corruption. FUI & IUF integer fui(float a) {//union float to integer integer b = (a<0) << 31; if((a = llFabs(a)) < 1.1754943508222875079687365372222e-38) b = b | (integer)((a * 8.5070591730234615865843651857942e+37) * 8388608.0); else { integer c = llFloor(llLog(a) / 0.69314718055994530941723212145818); b = b | ((c + 127) << 23 ) | (integer)(((a / llPow(2.0, c)) - 1) * 8388608.0); } return b; }
float iuf(integer a) {//union integer to float integer c= (a >> 23) & 0xff; float b = (a & 0x7fffff) / 8388608.0; if(c == 0xff) b = 1 / 0; //NaN or Infinity (since getting them crashes the script, thats we do). else if(c) b = (b + 1) * llPow(2, c - 127); else b = b / 8.5070591730234615865843651857942e+37; return b * ((a >> 31) | 1); }
_____________________
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
|
|
Strife Onizuka
Moonchild
Join date: 3 Mar 2004
Posts: 5,887
|
03-17-2006 23:47
float someFunction(float a, integer b) { float c = llPow(10, b); return llRound(b * c) / c; }
_____________________
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
|
|
Lex Neva
wears dorky glasses
Join date: 27 Nov 2004
Posts: 1,361
|
03-18-2006 11:02
From: Julian Fate What is the name of the operation that reduces the number of decimal places displayed in a number? Like, someFunction(0.2304923094, 4) would return 0.2304. I'm looking for what it's called when you do that. It isn't rounding or finding the floor but it has a name like those. This is rounding. Rounding just means finding a less exact (ie less precise, ie lower precision) representation of a number. This type of rounding is called truncation, as others pointed out.
|