Able to stop calculation returning 'e-002' at end?

Forum Home Forum Home > Construct Classic > Construct Classic Discussion > Help & Support using Construct Classic
 Post Reply Post Reply
Author
1,141 Rep
Post Options Post Options   Quote sparkfeather Quote  Post ReplyReply Direct Link To This Post Topic: Able to stop calculation returning 'e-002' at end?
    Posted: 17 Apr 2012 at 6:38am
It's extremely frustrating having my values randomly returning these
because it makes trying to balance the values and growth really hard
when it will be something like 0.16 and then 2.68e-002...
I have no idea what the actual values it is ranging from are and I
can't try and workout how to calculate that every single time it
changes, as it does so continuously.

I'd extremely appreciate if anyone knows how to stop this happening,
it's an absolute nightmare and I've lost far too much time over this.
Back to Top
6,514 Rep
Post Options Post Options   Quote 7Soul Quote  Post ReplyReply Direct Link To This Post Posted: 17 Apr 2012 at 7:09am
Also, sometimes I have a value and I add 0.1 every tick. If I print this value after 10 ticks, it will return 10.0000000000010 or something like that

Here's a capx
After more or less 5 seconds, it displays many zeroes

Also, notice that I used "every 1 second" but the timer seems a bit random, why is that?

Edited by 7Soul - 17 Apr 2012 at 7:18am
Back to Top
7,061 Rep
Post Options Post Options   Quote R0J0hound Quote  Post ReplyReply Direct Link To This Post Posted: 17 Apr 2012 at 7:50am
Use the "FormatDecimal()" system expression, it's output won't become 2.68e-002.
ex.
FormatDecimal(0.0268, 4) -> "0.0268"

@7Soul
That has to do with floating point precision.
Short Answer:
0.1 can't be exactly represented in binary, so a close approximation is used. When adding the number multiple times the inaccuracy grows. It is an issue with all computers. The number will not always be exact but it will be accurate to a certain number of digits, so the error is usually insignificant.

Long Answer:
http://docs.oracle.com/cd/E19957-01/806-3568/ncg_goldberg.html

Edited by R0J0hound - 17 Apr 2012 at 7:50am
Back to Top
6,514 Rep
Post Options Post Options   Quote 7Soul Quote  Post ReplyReply Direct Link To This Post Posted: 17 Apr 2012 at 5:54pm
@R0J0hound
There's no FormatDecimal on C2 :/
And thx for the explanation
Back to Top
1,141 Rep
Post Options Post Options   Quote sparkfeather Quote  Post ReplyReply Direct Link To This Post Posted: 18 Apr 2012 at 3:20am
Thank you very much :)

It seems to work, however.....What on earth is this message about
'Cannot add with integer and string. Operator had no effect' error
that is caused when using FormatDecimal?
As well as 'Cannot add with float and string. Operator had no effect'

Now it won't let me add that variable in other calculations??

Edited by sparkfeather - 18 Apr 2012 at 3:26am
Back to Top
5,270 Rep
Post Options Post Options   Quote tulamide Quote  Post ReplyReply Direct Link To This Post Posted: 18 Apr 2012 at 6:02am
FormatDecimal, as ROJO showed in his example and can be seen in the wizard, outputs a string, not a number. There's no need to convert a number using FormatDecimal into a string and then keep on calculating with it. Just calculate with the number itself, not its human readable text form.

btw, it isn't that hard to read. 2.68e-002 just means: shift the number 2.68 two decimal places to the right (2.68->0.268->0.0268)
It's the same with everything else. 2.68e-003 = shift 3 decimal places to the right = 0.00268, etc.
There's also the other way round. 1.5e+003 would mean, shift the number three decimal places to the left = 1500
Back to Top
1,141 Rep
Post Options Post Options   Quote sparkfeather Quote  Post ReplyReply Direct Link To This Post Posted: 18 Apr 2012 at 10:04am
Ahhh thank you, I hadn't realized that was meant to indicate it was being
turned into a string, just thought that was to highlight what the end result was.


Doing that would work if the number wasn't changing so radically as I mentioned,
which makes that more misleading/distracting than anything.
But I did get around it by as you said using the number itself in its
continued calculation, and I instead made another variable for it to
send the FormatDecimal result so I can keep an eye on it there :).

Thank you both very much for your help again, I can finally continue
moving forward with the project.
Back to Top
511 Rep
Post Options Post Options   Quote seridras Quote  Post ReplyReply Direct Link To This Post Posted: 18 Apr 2012 at 5:42pm
Originally posted by 7Soul 7Soul wrote:

@R0J0hound
There's no FormatDecimal on C2 :/
And thx for the explanation


I have tried using FormatDecimal to format text into currency, and always get an error for unknown expression.


Original format:

"$" &(Balance/100) &"." &right("00" &Balance%100,2)

has issues with displaying cents properly.

Looking for a better solution, or advice on what may be causing the error.
Back to Top
7,061 Rep
Post Options Post Options   Quote R0J0hound Quote  Post ReplyReply Direct Link To This Post Posted: 18 Apr 2012 at 7:42pm
@seridras
try this:
"$" &int(Balance) &"." &right("00" &(Balance*100)%100,2)
Back to Top
511 Rep
Post Options Post Options   Quote seridras Quote  Post ReplyReply Direct Link To This Post Posted: 18 Apr 2012 at 10:26pm
Adding int() did the trick.

I left off the (Balance*100)%100 since Balance was already storing cents in integer form (holdover from previous reference materials).

Thanks!
Back to Top
 Post Reply Post Reply

Forum Jump Forum Permissions View Drop Down