Digital Workshop

Welcome to the Digital Workshop Message Boards
It is currently December 23rd, 2024, 8:25 am

All times are UTC [ DST ]




Post new topic Reply to topic  [ 5 posts ] 
Author Message
 Post subject: Why does eval() script function introduce rounding errors?
PostPosted: December 25th, 2007, 6:48 am 
Offline
Godlike
Godlike

Joined: November 12th, 2005, 1:56 am
Posts: 1474
Location: SFBay Area
Opus: OpusPro v9.0x, & Evol.
OS: Vista32
System: Core 2 duo 2Ghz, RAM 3GB, Nvidia Go 7700 - laptop
Better said, how do I fix that?

It seems that using the eval() has a limit of 6-digit accuracy. I found that it was rounding-up or rounding-down 8-digit numbers (Color values).

Any thoughts where to code a fix? A simple example script follows.

Quote:
numTEST01 = aStoredNumA ;
//
eval( "numTEST" + "02" +'='+ aStoredNumA ) ;

//
//
// Have a persistent Page variable storing 1589487571 as a number.
// have page variables numTEST01 and numTEST02 showing different results.
//
// this simple test reproduces the issue I am seeing. Showed up with an 8-digit integer.
//
// numTEST01 returns the value correctly 1589487571
//
// numTEST02 returns a rounded-UP number: 1589490000
//
//

_________________
_good things come to those who wait(0)_


For this message Lar_123 has been thanked by : mackavi


Top
 Profile  
 
 Post subject:
PostPosted: December 26th, 2007, 7:11 pm 
Offline

Joined: November 11th, 2004, 4:05 am
Posts: 636
Location: Christchurch, NZ
If the eval function contains a string, any number with 7 digits or more in it will be "rounded" incorrectly.

For example, a number alone:
foo = eval(1589487571);
Debug.trace(foo); // get 1589487571

Now add a string, even an empty one::

foo = eval(""+1589487571);
Debug.trace(foo); // get 1589490000

You get a similar effect if you apply toString to a number.

n = 1589487571;
foo = n.toString();
Debug.trace(foo); // get 1589490000


cheers
Paul


For this message Paul has been thanked by : mackavi


Last edited by Paul on December 28th, 2007, 7:14 am, edited 1 time in total.

Top
 Profile  
 
 Post subject:
PostPosted: December 27th, 2007, 7:02 pm 
Offline
Godlike
Godlike
User avatar

Joined: March 21st, 2007, 10:44 am
Posts: 3188
Location: UK
Opus: Evolution
:-)

Wrong Eval syntax,

use

eval( "numTEST" + "02" +'= aStoredNumA' )

Happy holidays Lar

_________________
When you have explored all avenues of possibilities, what ever remains, how ever improbable, must be the answer.

Interactive Solutions for Business & Education
Learn Anywhere. Learn Anytime.

www.interaktiv.co.uk
+44 (0) 1395 548057


Top
 Profile Visit website  
 
 Post subject:
PostPosted: December 27th, 2007, 7:44 pm 
Offline

Joined: November 11th, 2004, 4:05 am
Posts: 636
Location: Christchurch, NZ
Good one Mac,
I shouldn't post when I'm full of turkey, christmas pudding and wine.

cheers
Paul


For this message Paul has been thanked by : mackavi


Top
 Profile  
 
 Post subject: Watch how using strings and eval. Watch syntax.
PostPosted: December 28th, 2007, 3:18 am 
Offline
Godlike
Godlike

Joined: November 12th, 2005, 1:56 am
Posts: 1474
Location: SFBay Area
Opus: OpusPro v9.0x, & Evol.
OS: Vista32
System: Core 2 duo 2Ghz, RAM 3GB, Nvidia Go 7700 - laptop
eval( "Thanks!" + 'Paul' +"and"+ 'Mac' ) ;

I really appreciate both the explanation and the fixes. I'm sure others will find this useful someday and will possibly keep them out of the deep end of trouble.

I am now getting into areas where I think I am using numbers or even boolean, only to find they're there as strings. So this small lesson in eval() illustrates one more potential hiccup.

Sure enough, Mac's syntax fix fixed this one in a snap.

Cheers.

_________________
_good things come to those who wait(0)_


For this message Lar_123 has been thanked by : mackavi


Top
 Profile  
 
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 5 posts ] 

All times are UTC [ DST ]


Who is online

Users browsing this forum: No registered users and 31 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
Jump to:  
cron
Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group