im working on creating a reproducible example but ...
# lucee
s
im working on creating a reproducible example but i have found that in a loop
Copy code
public void function testPrecisionEvaluateString(){
		
		var p1 = 23.74;
		var p2 = 37.05;

		var pe = precisionEvaluate('p1 + p2'); //works correctly
		assertEquals( pe, 60.79 );
	}
	
	public void function testPrecisionEvaluate(){
		
		var p1 = 23.74;
		var p2 = 37.05;

		var pe = precisionEvaluate(p1 + p2); //returns trailing decimal place .7900000001
		assertEquals( pe, 60.79 );
	}
What is the actual difference that could cause this?
a
I'm guessing it's cos with the latter one, yer using the CFML engine to add
p1 + p2
before doing the precision piece, whereas with the former the whole thing is being handled within the inner workings of
precisionEvaluate
. Just a guess though. I have NFI how
precisionEvaluate
works. (and trycf.com is not helping here... /me shakes fist at Abram, whilst laughing)
Cranking up my local Lucee instance...
s
oh, that makes a lot of sense, yeah i ran into the same with trycf, im sure it saw evaluate and bailed
✅ 1
b
Yeah, you have to pass in a string otherwise it defeats the purpose
You're adding the numbers first on their own and then just passing in the result to precisioneval which isn't what you want.
s
the docs are confusing because it only shows raw numbers as opposed to variables so i didnt realize it had to be a string that included variables
Copy code
precisionEvaluate('a + b')
a
I had something in the back of my mind that
precisionEvaluate
did similar sort of magic to
parameterExists
or
isNull
and somehow didn't evaluate the expression before it was passed into it. If that makes sense.
But that's based on nothing. Think I've only ever used it once in production code.
Have just been reminded how
===
is broke on Lucee. Sigh.
For me:
Copy code
p1 = 23.74;
p2 = 37.05;
f = p1 + p2
expected = 60.79
pe = precisionEvaluate(p1 + p2); //returns trailing decimal place .7900000001

writeDump([
    expected = expected, // 60.79
    "expected.toString()" = expected.toString(), // 60.79
    "pe" = pe, // 60.79
    "pe.toString()" = pe.toString(), // 60.79
    "f.toString()" = f.toString() // 60.78999999999999
])

https://i.paste.pics/7f5278a4947b7104e84c92e2868f7554.png▾

👍🏾 1