Raised This Month: $51 Target: $400
 12% 

SM Dynamic Array Float Math Issue


Post New Thread Reply   
 
Thread Tools Display Modes
Author Message
Malachi
Senior Member
Join Date: Jun 2010
Location: USA
Old 05-27-2014 , 15:38   SM Dynamic Array Float Math Issue
Reply With Quote #1

So I had this crazy problem yesterday that I just figured out, and wanted to know if this is an issue or if I just missed something.

Works:
Code:
new Float:A[SIZE];
...
A[Index] += 25.0;
Doesn't work:
Code:
new Float:A[SIZE];
    myArray = CreateArray(1);
    PushArrayCell(myArray, 25.0);
...
A[Index] += GetArrayCell(myArray, Index);
Also doesn't work:
Code:
new Float:A[SIZE];
    myArray = CreateArray(1);
    PushArrayCell(myArray, 25.0);
...
A[Index] = GetArrayCell(myArray, Index) + A[Index];
Also also doesn't work:
Code:
new Float:A[SIZE];
    myArray = CreateArray(1);
    PushArrayCell(myArray, 25.0);
...
A[Index] = float(GetArrayCell(myArray, Index)) + A[Index];
But this works:
Code:
new Float:A[SIZE];
    myArray = CreateArray(1);
    PushArrayCell(myArray, 25.0);
...
A[Index] = FloatAdd(GetArrayCell(myArray, Index), A[Index]);

So I've never seen that before. Is this a problem w/SM? Or am I just missing something?

And is this the proper way to do this?

Thanks!
Malachi is offline
Malachi
Senior Member
Join Date: Jun 2010
Location: USA
Old 05-27-2014 , 15:42   Re: SM Dynamic Array Float Math Issue
Reply With Quote #2

Almost forgot:
SourceMod Version Information:
SourceMod Version: 1.5.3
SourcePawn Engine: SourcePawn 1.1, jit-x86 (build 1.5.3)
SourcePawn API: v1 = 4, v2 = 4
Compiled on: Mar 21 2014 07:50:09
Build ID: 4026:53569c20fc33
http://www.sourcemod.net/

Metamod:Source version 1.10.0
Build ID: 860:a58a1912f602
Loaded As: Valve Server Plugin
Compiled on: Aug 25 2013
Plugin interface version: 15:14
SourceHook version: 5:5
http://www.metamodsource.net/


TF2 linux server.
Malachi is offline
Powerlord
AlliedModders Donor
Join Date: Jun 2008
Location: Seduce Me!
Old 05-27-2014 , 16:06   Re: SM Dynamic Array Float Math Issue
Reply With Quote #3

When you pull a Float value back out of an adt_array, you must use the Float tag:
PHP Code:
Float:GetArrayCell(myArrayIndex
If you don't, it thinks you're pulling back an integer and will then try to convert said integer value to a Float value using float() (int + float always promotes the int to a float first and returns a float), which doesn't resemble the original value at all because it already was a Float but SourceMod didn't know it.

So, likely what you want is this:

PHP Code:
new Float:A[SIZE];
    
myArray CreateArray(1);
    
PushArrayCell(myArray25.0);
...
A[Index] += Float:GetArrayCell(myArrayIndex); 
__________________
Not currently working on SourceMod plugin development.

Last edited by Powerlord; 05-27-2014 at 16:24.
Powerlord is offline
Bacardi
Veteran Member
Join Date: Jan 2010
Location: mom's basement
Old 05-27-2014 , 16:11   Re: SM Dynamic Array Float Math Issue
Reply With Quote #4

nvm

Last edited by Bacardi; 05-27-2014 at 16:12.
Bacardi is offline
Powerlord
AlliedModders Donor
Join Date: Jun 2008
Location: Seduce Me!
Old 05-27-2014 , 17:07   Re: SM Dynamic Array Float Math Issue
Reply With Quote #5

As for why float() doesn't work deals with how the number formats work. Both integer and Float in pawn are 32-bits long.

integer (the normal cell value type) is:
1 bit for sign
31 bits for value

Float is:
1 bit for sign
8 bits for exponent (power + 127)
23 bits for the Mantissa/significant... or the remaining value

For now, lets assume we're dealing with the number 1 (or 1.0). Here's how it looks in binary

0000 0000 0000 0000 0000 0000 0000 0001

It works a bit differently for floating point...

Since we already know the binary representation, we take that, convert it to scientific notation (1.0 * 2^0) and add 127 to the power (0 + 127 = 127)... so....
0 01111111 00000000000000000000000
(remember the "1." is dropped from the mantissa because it's part of the power)

If you treat the representation of 1.0 as a integer, you end up with 1065353216 and float(1065353216) just gives you 1065353216.0 (1.111111 * 2^29 or 0 10011100 11111100000000000000000) instead of 1.0.
__________________
Not currently working on SourceMod plugin development.

Last edited by Powerlord; 05-27-2014 at 17:14.
Powerlord is offline
Malachi
Senior Member
Join Date: Jun 2010
Location: USA
Old 05-27-2014 , 17:23   Re: SM Dynamic Array Float Math Issue
Reply With Quote #6

Quote:
Originally Posted by Powerlord View Post
When you pull a Float value back out of an adt_array, you must use the Float tag:
PHP Code:
Float:GetArrayCell(myArrayIndex

Ah crap, I actually looked for that (I knew about the _: tag) - but didnt' find it.

Ok, I'm going to try that (and hopefully feel like things make sense again).

Thanks!
(and a very fast reply too)
Malachi is offline
Malachi
Senior Member
Join Date: Jun 2010
Location: USA
Old 05-27-2014 , 17:39   Re: SM Dynamic Array Float Math Issue
Reply With Quote #7

Confirmed that this appears to be working.

Thanks again!
Malachi is offline
Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT -4. The time now is 18:00.


Powered by vBulletin®
Copyright ©2000 - 2024, vBulletin Solutions, Inc.
Theme made by Freecode