Page MenuHomeFeedback Tracker

Significant Figures (issue with Cos and Sin)
New, WishlistPublic

Description

Hello,

I have building script that allows players to move objects around. When you use SIN and COS (to get the ratio of the distance to move based upon the direction the object is moving) all works fine until you divide(or multiply by .99999 or less)...

Details

Legacy ID
412598237
Severity
None
Resolution
Open
Reproducibility
Always
Category
Scripting
Steps To Reproduce

For instance:

SIN of 52 degrees is: 0.78801075360672195669397778783585
COS of 52 degrees is: 0.61566147532565827966881109284366

if I wanted to move the object 1m in the forward direction, I would do the following:

_pos = getPosWorld Object;
_xpos = _pos select 0;
_ypos = _pos select 0;
_zpos = _pos select 0;

Object setPositonWorld [_xpos + (cos(52) * 1),_ypos + (cos(52) * 1),_zpos];

this would move the object forward in the direction it's heading.

if I want to do it by .1 instead of 1 is where the issue is.

Since the significant values are only 6 places, my COS and SIN are:
SIN of 52 degrees is: 0.78801
COS of 52 degrees is: 0.61566

when I take 1/10th of this value I get:
SIN of 52 degrees * 1/10th: 0.07880
COS of 52 degrees * 1/10th: 0.06156

This skews the direction of the object and makes it move a little right or left.

This is not precise enough, I suggest bringing it to 10 significant values.

Event Timeline

blckeagls edited Additional Information. (Show Details)
blckeagls set Category to Scripting.
blckeagls set Reproducibility to Always.
blckeagls set Severity to None.
blckeagls set Resolution to Open.
blckeagls set Legacy ID to 412598237.May 7 2016, 8:17 PM
blckeagls added a subscriber: blckeagls.

My Code:

private ["_Dik","_DikCode","_acceptableCodes","_add","_addtoXfront","_addtoXside","_addtoYfront","_addtoYside","_altState","_bldgDir","_bldgDir-90","_cosfront","_cosside","_ctrlState","_pos","_shiftState","_sinfront","_sinside","_this"];
systemChat(format["_this passed: %1",_this]);
_Dik = _this select 0;
systemChat(format["BuildObj passed: %1",BuildObj]);
_DikCode = _Dik select 1; integer
systemChat(format["_DikCode passed: %1",_DikCode]);
_shiftState = _Dik select 2; boolean
_ctrlState = _Dik select 3;
boolean
_altState = _Dik select 4; // boolean
_acceptableCodes = [199,210,207,211,16,18,201,209];

if (!(_DikCode in _acceptableCodes)) exitWith {};

_add = .001;
if (_shiftState) then {
_add = .01;
};
if (_ctrlState) then {
_add = .1;
};
if (_altState) then {
_add = 1;
};

_pos = getPosWorld BuildObj;
_xpos = _pos select 0;
_ypos = _pos select 1;
_zpos = _pos select 2;

if (_DikCode == 201) then {
BuildObj setPosWorld [_xpos,_ypos,_zpos + _add];
};
if (_DikCode == 209) then {
BuildObj setPosWorld [_xpos,_ypos,_zpos - _add];
};

_add = 1;
if (_shiftState) then {
_add = 5;
};
if (_ctrlState) then {
_add = 15;
};
if (_altState) then {
_add = 45;
};

if (_DikCode == 16) then {
["F",_add] execVM "A3ResourceWarCl\PlayerScripts\building\vectors\Yaw.sqf";
};
if (_DikCode == 18) then {
["B",_add] execVM "A3ResourceWarCl\PlayerScripts\building\vectors\Yaw.sqf";
};

_add = .01;
if (_shiftState) then {
_add = .1;
};
if (_ctrlState) then {
_add = 1;
};
if (_altState) then {
_add = 4;
};

_sinside = 0.000000000000000 + sin(Bldgyaw);
systemChat(format["_sinside: %1",_sinside]);
_cosside = 0.000000000000000 + cos(Bldgyaw);
systemChat(format["_cosside: %1",_cosside]);
_sinfront = 0.000000000000000 + sin(Bldgyaw-90);
systemChat(format["_sinfront: %1",_sinfront]);
_cosfront = 0.000000000000000 + cos(Bldgyaw-90);
systemChat(format["_cosfront: %1",_cosfront]);

if (_DikCode == 199) then {
BuildObj setPosWorld [_xpos + (_sinside * _add),_ypos + (_cosside * _add),_zpos];
};
if (_DikCode == 207) then {
BuildObj setPosWorld [_xpos - (_sinside * _add),_ypos - (_cosside * _add),_zpos];
};
if (_DikCode == 210) then {
BuildObj setPosWorld [_xpos + (_sinfront * _add),_ypos + (_cosfront * _add),_zpos];
};
if (_DikCode == 211) then {
BuildObj setPosWorld [_xpos - (_sinfront * _add),_ypos - (_cosfront * _add),_zpos];
};