Ambiera Forum

Discussions, Help and Support.

Ambiera Forum > CopperCube > CopperCube Open discussion
Javascript bugs - be warned

Robo
Guest
Quote
2020-11-11 14:06:20

I was accumulating info the last few months on how to encourage users to code in javascript but over time I realize javascript is really a pain in the arse and I honestly cant recommend it.

It fails to recognize numbers when using numbers at times, and not a string, using "+" to add numbers together sometimes wont add them but joins them together instead thinking its a text not a number...and now I find javascript cant even calculate decimals properly...WTF....yes, its all true..

Javascript cannot calculate friggin simple decimal calculations without stuffing up. It adds a rounding number in even when incorrect to do so. The result is either a very long decimal or NAN (not a number) returned when you want to only have 1 decimal place using ".toFixed(1)".

Don't believe me - read this:
http://adripofjavascript.com/blog/drips/avoiding-problems-with-decimal-math-in-javascript.html


Robo
Guest
Quote
2020-11-11 15:15:28

Shouldn't have to work around Javascript issues but you do.

If you need to use decimals in your game and have it work as intended then one of the best solutions is to multiply the decimal by a factor of 10 or 100 to get to an integer using 'Math.round' then revert back again to decimal whole number:

Math.round(value*100)/100;
// 10 defines 1 decimals, 100 for 2, 1000 for 3


marcw
Registered User
Quote
2020-11-15 21:04:36

Hello,

I am a newbie in JS.
I tried this code :

var worfsMoney = (600.9).toPrecision(8);
var bloodWinePrice = (200.3).toPrecision(8);
var worfsTotal = (bloodWinePrice * 3).toPrecision(8);

worfsMoney = worfsTotal;
worfsTotal;
(worfsTotal * 3).toPrecision(8);



https://1drv.ms/u/s!AjokN1FU3jK1km4Wn_Pl1NgJPggK?e=4NJgxK


Robo
Guest
Quote
2020-11-17 01:18:11

wrote:
Hello,

I am a newbie in JS.
I tried this code :

var worfsMoney = (600.9).toPrecision(8);
var bloodWinePrice = (200.3).toPrecision(8);
var worfsTotal = (bloodWinePrice * 3).toPrecision(8);

worfsMoney = worfsTotal;
worfsTotal;
(worfsTotal * 3).toPrecision(8);



https://1drv.ms/u/s!AjokN1FU3jK1km4Wn_Pl1NgJPggK?e=4NJgxK


thanks = yes, that might help some people, but my main problem is saving game data from an array and loading back up again.

Both '.toFixed. and also ..toPrecision' have limitations on what numbers it can handle before using exponential notation like for example '1.2e+3'. Once the decimal is represented as exponential notation and saved - once reloaded it is no longer recognizable as a number by CopperCube and breaks the code..


srfstudio
Registered User
Quote
2020-11-17 13:00:18

I'm formatting variables as numbers with Number(variable), I'm not sure if you tried it that way, maybe it didn't work out in your script for some reason.
var nameadd_value = Number(nameadd_var + 1);
I generally use it in all scripts that have issues in WebGL exports. Here's my old cloning/renaming script that worked both in WebGL & Win:
https://www.ambiera.com/forum.ph...
WebGL also has a problem with custom script references, nodes defined by/through custom scripts.


marcw
Registered User
Quote
2020-11-17 13:33:31

Robo and Srfstudio, thank you for your interesting comments.


About the .toPrecision() method, I noticed that it is not as accurate as expected when working with large numbers.

var worfsMoney = 10.12345 ** 32;
var bloodWinePrice = 10.12345 ** 31;
var worfsTotal = bloodWinePrice * 10.12345;

n1 = worfsTotal.valueOf();
n2 = worfsMoney.valueOf();

if (n2 == n1) {
('true');
} else {
('not true');
}
n2 - n1;

n3 = worfsTotal.toPrecision(40);
n4 = worfsMoney.toPrecision(40);

if (n4 == n3) {
('true');
} else {
('not true');
}
n4 - n3;





Robo
Guest
Quote
2020-11-17 23:33:22

wrote:
I'm formatting variables as numbers with Number(variable), I'm not sure if you tried it that way, maybe it didn't work out in your script for some reason.
var nameadd_value = Number(nameadd_var + 1);
I generally use it in all scripts that have issues in WebGL exports. Here's my old cloning/renaming script that worked both in WebGL & Win:
https://www.ambiera.com/forum.ph...
WebGL also has a problem with custom script references, nodes defined by/through custom scripts.


I like your answer - seems a good one. I was just multiplying the value by 1 to ensure its a number but might use what you suggest instead, maybe that way I can still use "+" to only add values and not join together like a string...

cheers !


Guest
Guest
Quote
2020-12-24 10:42:48

In JavaScript vars are not simple cars but objects


Create reply:


Posted by: (you are not logged in)


Enter the missing letter in: "Internation?l" (you are not logged in)


Text:

 

  

Possible Codes


Feature Code
Link [url] www.example.com [/url]
Bold [b]bold text[/b]
Image [img]http://www.example.com/image.jpg[/img]
Quote [quote]quoted text[/quote]
Code [code]source code[/code]

Emoticons


   






Copyright© Ambiera e.U. all rights reserved.
Privacy Policy | Terms and Conditions | Imprint | Contact