JavaScript Question

Monkey Archive Forums/Digital Discussion/JavaScript Question

impixi(Posted 2012) [#1]
I'm coding an opengl-free, IE9-friendly HTML5 databuffer class to overcome the limitations of monkey's inbuilt version. I've got everything working, except PeekFloat/PokeFloat.

The problem I'm having is converting floats to ints and vice-versa without losing precision. I've searched around and found a couple of supposed solutions, but they seem particularly complicated for my purposes.

So I'm wondering if anyone knows a simple JavaScript solution to convert a IEE754 Float to a 32bit Integer and then convert it back again without losing precision? Preferably using only bit shifting and masking operations? Anyone?

Here's my code so far:

htmldatabuffer.js


htmldatabuffer.monkey


test.monkey



muddy_shoes(Posted 2012) [#2]
As javascript doesn't really have integers and floats, only 64-bit doubles, and it just pretends that anything that you use a bitwise operator on is a 32-bit integer, I doubt there is a simple way to do this.

Here's some reading material: http://www.hunlock.com/blogs/The_Complete_Javascript_Number_Reference


impixi(Posted 2012) [#3]
Thanks for the link. That's a nice gathering in one place of all the bits and pieces of information I've been reading regarding Javascript numbers and bitwise manipulation.

Looks like I'll have to trawl through this guy's code to see if I can extract something usable:

http://www.merlyn.demon.co.uk/js-exact.htm#IEEE

Could take me a while though... And the resultant code will not be quick.


AdamRedwoods(Posted 2012) [#4]
ieee four byte (int) to float converter written in monkey:
http://www.monkeycoder.co.nz/Community/posts.php?topic=2465