aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authoralon@honor <none@none>2010-09-24 20:47:55 -0700
committeralon@honor <none@none>2010-09-24 20:47:55 -0700
commit05b2fafe901cf658c9f1acb80757f06453aacc62 (patch)
tree8d79a9d5277b24360c5b710627bc903850fb2bb6
parent9352899596e6ee328824117031fc56fd2123336e (diff)
floating point fixes +test
-rw-r--r--src/jsifier.js2
-rw-r--r--src/parseTools.js3
-rw-r--r--src/preamble.js12
-rw-r--r--tests/runner.py4
4 files changed, 16 insertions, 5 deletions
diff --git a/src/jsifier.js b/src/jsifier.js
index c9c2a63b..d4cfc07a 100644
--- a/src/jsifier.js
+++ b/src/jsifier.js
@@ -490,6 +490,8 @@ function JSify(data) {
});
makeFuncLineZyme('mathop', function(item) { with(item) {
dprint('mathop', 'mathop: ' + dump(item));
+ ident = parseNumerical(ident);
+ ident2 = parseNumerical(ident2);
switch (item.op) {
case 'add': return ident + ' + ' + ident2;
case 'sub': return ident + ' - ' + ident2;
diff --git a/src/parseTools.js b/src/parseTools.js
index 456b44f1..f0ac834c 100644
--- a/src/parseTools.js
+++ b/src/parseTools.js
@@ -335,7 +335,8 @@ function IEEEUnHex(stringy) {
}
}
e -= 1023; // offset
- return (((((a & 0xfffff | 0x100000) * 1.0) / Math.pow(2,52-32)) * Math.pow(2, e)) + (((b * 1.0) / Math.pow(2, 52)) * Math.pow(2, e)) * (neg ? -1 : 1)).toString();
+ var absolute = ((((a & 0xfffff | 0x100000) * 1.0) / Math.pow(2,52-32)) * Math.pow(2, e)) + (((b * 1.0) / Math.pow(2, 52)) * Math.pow(2, e));
+ return (absolute * (neg ? -1 : 1)).toString();
}
function parseNumerical(value, type) {
diff --git a/src/preamble.js b/src/preamble.js
index a2ceaa0c..e458384d 100644
--- a/src/preamble.js
+++ b/src/preamble.js
@@ -136,8 +136,16 @@ function __formatString() {
while (curr != 0) {
curr = HEAP[textIndex];
next = HEAP[textIndex+1];
- if (curr == '%'.charCodeAt(0) && ['d', 'f'].indexOf(String.fromCharCode(next)) != -1) {
- String(arguments[argIndex]).split('').forEach(function(chr) {
+ if (curr == '%'.charCodeAt(0) && ['d', 'f', '.'].indexOf(String.fromCharCode(next)) != -1) {
+ var argText = String(arguments[argIndex]);
+ // Handle very very simply formatting, namely only %.Xf
+ if (HEAP[textIndex+1] == '.'.charCodeAt(0)) {
+ var limit = parseInt(String.fromCharCode(HEAP[textIndex+2]));
+ var dotIndex = argText.indexOf('.');
+ argText = argText.substr(0, dotIndex+1+limit);
+ textIndex += 2;
+ }
+ argText.split('').forEach(function(chr) {
ret.push(chr.charCodeAt(0));
});
argIndex += 1;
diff --git a/tests/runner.py b/tests/runner.py
index 28f136ca..1149bfd9 100644
--- a/tests/runner.py
+++ b/tests/runner.py
@@ -153,11 +153,11 @@ class T(unittest.TestCase):
#include <cmath>
int main()
{
- printf("*%f\\n", M_PI);
+ printf("*%.2f,%.2f*\\n", M_PI, -M_PI);
return 0;
}
'''
- self.do_test(src, '*3.14159')
+ self.do_test(src, '*3.14,-3.14*')
def test_if(self):
src = '''