Working with integers

Hi all,

Quite frequently I find myself working integers on various programming tasks. Coming from the previous makers working with integers in Ruby is really easy. The seamless conversion between Fixnum and Bignum meant that as a scripter you didn't even need to care about integer overflow.

As I understand it there is no base integer type in JavaScript, only a floating point value. How do you javascripters typically work in scenarios where you conceptually want integers?

Are there best practices out there? Some nice libraries?

I'd really rather not deal with the complexities of floating points when I don't actually need the decimals.

*hugs*

 - Zeriab
I know its from W3 Schools and I hate my self already but: http://www.w3schools.com/jsref/jsref_parseint.asp the main concept is parseInt. This is what I use some times when I need to make sure what I am working with is an integer.

There is also a whole section here on numbers. (Thank god for MDN)

Just ve very careful and ALWAYS.....ALWAYS use === or !== for truthy statements, never use == in Javascript, especially when it comes to integers and strings because this way you know 100% what your comparing is either true or false.
actually the best ways of "simulate" integer in JS is to use this :

var float = 1.23;var integer = Math.floor(float);it's the ways to convert float number into Integer 

but after you can have library who will help you with integer manipulation 

http://silentmatt.com/biginteger/

but after it's not all floats JS is a bit weirds...

1 = 1.5;//but2 = 2;/* it's kinda weird thing's they wanted to use a dynamic enumeration but they messed up this but example you will do 1.5 + 1.5 it's will equal 3 but if you do 1 + 1 it's will equal 2.so maths like this :*/1 + 2 = 3;like I said maths in JS are kinda REALLY weird lol's
actually the best ways of "simulate" integer in JS is to use this :

var float = 1.23;var integer = Math.floor(float);it's the ways to convert float number into Integer 

but after you can have library who will help you with integer manipulation 

http://silentmatt.com/biginteger/

but after it's not all floats JS is a bit weirds...

1 = 1.5;//but2 = 2;/* it's kinda weird thing's they wanted to use a dynamic enumeration but they messed up this but example you will do 1.5 + 1.5 it's will equal 3 but if you do 1 + 1 it's will equal 2.so maths like this :*/1 + 2 = 3;like I said maths in JS are kinda REALLY weird lol's

Math in JS is odd yes, but I would use parseInt:

The parseInt() function parses a string argument and returns an integer of the specified radix (the base in mathematical numeral systems).

and Not Math.floor because that can give you some really bizzar values back. You can also use: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Number/isInteger. If you need the floating number you can use https://developer.mozilla.org/en/docs/Web/JavaScript/Reference/Global_Objects/parseFloat

How ever I suggest that unless you are rounding up or down (ciel, floor) or rounding (round) then I suggest you stay away from those and use appropriate Number methods like the ones I listed here to validate that yes I have a float, integer or what have you. its"safer" and you wont run into , why is my 1.2457 now a 1.3 or some other weird issue with Math.functionName. floor will also turn 1.2457 into 1.2 which you might not want.
Is == ever useful for anything in for JavaScript (I still have not found a use for it yet)?

In regards to the topic, beware of rounding when trying to get integers (screwed me over way too many times in the past).

p.s. I love truncation 
Is == ever useful for anything in for JavaScript (I still have not found a use for it yet)?

In regards to the topic, beware of rounding when trying to get integers (screwed me over way too many times in the past).

p.s. I love truncation 

== is a way to say "is equals" which is common in ... every other language how ever in javscript (and php for example) === states "type of x" MUST equal "type of x" in javascript people tend to do == because they do not understand that they must always do === and thus when they get odd results its harder to debug.

Short answer: no.
If you do work with floats, don't forget to always round anything after the 12th decimal place, as they are often wrong. While working on my pixel movement / collision script, I ran into a few issues because JS doesn't round numbers automatically like pretty much every other language does.
yes rounding number round really WEIRD in JS  this why you may works with a 1.5 logics sometime or use the method you mentionned

although for awesome :

"==" is a non strict equal

"===" is a strict equal

you may only use "==" when you are sure you want to left a little margin of errors in your result but yes always use "===" '3' it's better
yes rounding number round really WEIRD in JS  this why you may works with a 1.5 logics sometime or use the method you mentionned

although for awesome :

"==" is a non strict equal

"===" is a strict equal

you may only use "==" when you are sure you want to left a little margin of errors in your result but yes always use "===" '3' it's better
"==" will match even if the type of the variable is different, while "===" will also check the type:

Code:
15 == "15"15 !== "15"
If you do work with floats, don't forget to always round anything after the 12th decimal place, as they are often wrong. While working on my pixel movement / collision script, I ran into a few issues because JS doesn't round numbers automatically like pretty much every other language does.
Does any one ever let anything go that high? I usuaully stop at the 6th as you don't need that much precision.

yes rounding number round really WEIRD in JS  this why you may works with a 1.5 logics sometime or use the method you mentionned

although for awesome :

"==" is a non strict equal

"===" is a strict equal

you may only use "==" when you are sure you want to left a little margin of errors in your result but yes always use "===" '3' it's better
Also all scripts, this is off topic, but all scripts need to have: 'use strict' in them to enforce good javascript standards. Sorry for the off topic. Back to integers and floats 😀DDDD

Also on your edited comment: the reason !== works is because as you said Type checking. Type checking is crucial in JS
Does any one ever let anything go that high? I usuaully stop at the 6th as you don't need that much precision.
What happens if I accept damage formulas like

Code:
11 / 3
And then do something with it without properly rounding it?
What happens if I accept damage formulas like

11 / 3And then do something with it without properly rounding it?
Every use cause is different so in your case you might want to use parseInt() which gets you the integer representation, you could also use Math.floor or Math.ciel depending on how you wnat the damage to be represented. There are use cases for rounding which I did not  say to not use. it depends on the situation.

So for damage:

11/33.6666666666666665Math.floor(11/3)3Math.ceil(11/3)4Math.round(11/3)4Again it depends on what you want the out come to be ... 

parseInt(11/3)3parseFloat(11/3)3.6666666666666665Rounding should be used where appropriate. Just be super super careful
I approve what's darkness say you don't exactly need to use Floor or ciel parsing ect everywhere's 

likes if you want to place something on the screen...you don't actually need to use Math.floor 

likes 

this.sprite.x = 100;or 

this.sprite.x = Graphics.width / 2you don't need something likes maths.floor for something so basic c:
I approve what's darkness say you don't exactly need to use Floor or ciel parsing ect everywhere's 

likes if you want to place something on the screen...you don't actually need to use Math.floor 

likes 

this.sprite.x = 100;or 

this.sprite.x = Graphics.width / 2you don't need something likes maths.floor for something so basic c:

I would recomend though a parseInt on that value for this.spite.x because in this case you want whole numbers not round. You could Use Math.floor/ceil if you know what you want in that case, how ever I would use parseInt
oh actually Graphics.width is already Using Maths.floors for round the number if I remember correct

(you can see it in the demo code)
oh actually Graphics.width is already Using Maths.floors for round the number if I remember correct

(you can see it in the demo code)

good 😀
Does any one ever let anything go that high? I usuaully stop at the 6th as you don't need that much precision.
Probably not, but up to the 12th decimal place the values are still reliable, so there's no need.
oh actually Graphics.width is already Using Maths.floors for round the number if I remember correct


(you can see it in the demo code)
That doesn't necessarily mean you should do the same thing.
That doesn't necessarily mean you should do the same thing.
um theres a 99% reason why they did that way. can you give me an example where - in that specific case - you might want to use the decimal value to move a character or sprite .x pixels?
Thanks a bunch guys :3

um theres a 99% reason why they did that way. can you give me an example where - in that specific case - you might want to use the decimal value to move a character or sprite .x pixels?
If you are working with projections. You know, the typical transformation when dealing using an affine transformation matrix, translate, rotate, scale, shear, reflect. Particularly shear and rotate can benefit from decimal values.

Original thread: https://forums.rpgmakerweb.com/threads/working-with-integers.46200/