Source language: Translate to:

Elapsed time 00:00:00 - timer calculation error

Questions about using NeoBook's scripting language

Moderator: Neosoft Support

Elapsed time 00:00:00 - timer calculation error

Postby RickOShay » Sun Apr 29, 2012 8:24 am

I have a simple subroutine that calculates the elapsed time in Hours Minutes and Seconds.
The routine runs every 1000 ms using a timer. Variable [P1TimeE] = Player 1 Time elapsed in secs. (i.e. this not the variable that stores the elapsed time in ms but a variable that increments by one every 1000ms)

A similar routine used to work in the previous version of Neobook - now I get weird results.

I saw somewhere that the Maths calc has changed in 5.7.1
So what am I doing wrong? I've tried many different methods and each time I get the wrong result.


Code: Select all
Math "[P1TimeE]+1" "0" "[P1TimeE]"

. convert elapsed time into hours, minutes & seconds for display

. take total elapsed seconds and divide by # secs in an hour to get hrs
Math "trunc([P1TimeE]/3600)" "-1" "[P1Hrs]"
Math "trunc([P1Hrs]*3600)" "-1" "[P1secsinHrs]"

.take seconds in elapsed hours and subtract from total elapsed time
Math "trunc([P1TimeE]-[P1secsinHrs])" "-1" "[P1TimeRem]"

.take remainder and divide by secs in hour to get mins
Math "trunc([P1TimeRem]/60)" "-1" "[P1Mins]"
Math "trunc([P1Mins]*60)" "-1" "[P1secsinMins]"

.get remainder secs
Math "trunc([P1TimeRem]-[P1secsinMins])" "-1" "[P1Secs]"

.change display in case secs = 60
If "[P1Secs]" "=" "60"
Setvar "[P1Mins]" "1+[P1Mins]"
Endif

[b].add extra zero in display[/b]
If "[P1Secs]" "<" "10"
Setvar "[P1Secs]" "0[P1secs]"
Endif

. change mins and hrs in case mins = 1 hr
If "[P1Mins]" "=" "60"
Setvar "[P1Mins]" "00"
Setvar "[P1Hrs]" "1+[P1Hrs]"
Endif

.add extra zero in display
If "[P1Mins]" "<" "10"
Setvar "[P1Mins]" "0[P1mins]"
Endif

.add extra zero in display
If "[P1Hrs]" "=" "0"
Setvar "[P1Hrs]" "00"
Endif

.output string
SetVar "[P1TimeElapsed]" "[P1Hrs]:[P1Mins]:[P1Secs]"
Last edited by RickOShay on Mon Apr 30, 2012 5:04 am, edited 1 time in total.
There's no place like 127.0.0.1

http://www.farenheit451.net
User avatar
RickOShay
 
Posts: 22
Joined: Sun Jul 27, 2008 5:59 am
Location: Cape Town, SA

Postby Gaev » Sun Apr 29, 2012 12:50 pm

RickOShay:
A similar routine used to work in the previous version of Neobook - now I get weird problems.
I saw somewhere that the Maths calc has changed in 5.7.1
So what am I doing wrong? I've tried many different methods and each time I get the wrong result.
1) [P1TimeE] is returned in milliseconds ... not seconds ... so you have to divide it by 1000 first in order to get the number of elapsed seconds.

2) What is the reason for the first command ... where you add 1 to [P1TimeE] ?

3) Why don't you post your results here ... start with [P1TimeE] and [P1TimeElapsed] ... and if that does not give us a clue, may have to ask for the other variables as well.

Also, NeoBook would balk at this line ...
Code: Select all
[b].add extra zero in display[/b]
User avatar
Gaev
 
Posts: 3718
Joined: Fri Apr 01, 2005 7:48 am
Location: Toronto, Canada

Try...

Postby virger » Sun Apr 29, 2012 9:10 pm

{NeoBook 5 Objects}
NeoBookVer=5.70
ObjectType=3
Name=BtInit
X=448
Y=82
W=191
H=121
Anchor=0
Text=[iTime]¶[Elapsed]¶[th]:[tm]:[ts]
Align=2
ImageStyle=0
XPTheme=Yes
ObjAction=.. SIMULA CHANGE THE TIME TO 20¶timerstart "TM" "1000"
LineColor=0
LineWidth=1
LineStyle=0
FillColor=16314600
FillPattern=0
Font=Arial
FontSize=16
FontStyle=0
FontCharset=1
TextColor=0
TabOrder=29
ObjectType=15
Name=TM
X=603
Y=167
W=32
H=32
Anchor=0
AutoStart=No
AutoStop=No
Interval=1000
ObjAction=if "[iTime]" "=" ""¶ math "([Hour]*3600)+([Minute]*60)+[Second]" "" "[iTime]"¶..SIMULA¶. CHNAGE THE "TIME" TO 20¶. setvar "[xHour]" "[Hour]"¶. setvar "[xMinute]" "[Minute]"¶. setvar "[xSecond]" "[Second]"¶. setvar "[xMinute]" "56"¶. setvar "[xHour]" "21"¶. math "([xHour]*3600)+([xMinute]*60)+[xSecond]" "" "[iTime]"¶. setvar "[cc]" "0"¶. math "[xMinute]+53" "" "[xMinute]"¶. math "[xHour]+12" "" "[xHour]"¶else¶..SIMULA¶. math "[cc]+1" "" "[cc]"¶. if "[cc]" "=" "3"¶. math "[xSecond]+1" "" "[xSecond]"¶. if "[xSecond]" "=" "60"¶. setvar "[xSecond]" "0"¶. math "[xMinute]+1" "" "[xMinute]"¶. if "[xMinute]" "=" "60"¶. math "[xHour]+1" "" "[xHour]"¶. setvar "[xMinute]" "0"¶. endif¶. endif¶. setvar "[cc]" "0"¶. endif¶. math "(([xHour]*3600)+([xMinute]*60)+[xSecond])-[iTime]" "" "[Elapsed]"¶¶ math "(([Hour]*3600)+([Minute]*60)+[Second])-[iTime]" "" "[Elapsed]"¶ math "Trunc([Elapsed]/3600)" "" "[hh]"¶ math "Trunc(([Elapsed]-([hh]*3600))/60)" "" "[mm]"¶ math " [Elapsed]-( ([hh]*3600) + ([mm]*60) )" "" "[ss]"¶¶ if "[ss]" "<" "10"¶ setvar "[ts]" "0[ss]"¶ else¶ setvar "[ts]" "[ss]"¶ endif¶ if "[mm]" "<" "10"¶ setvar "[tm]" "0[mm]"¶ else¶ setvar "[tm]" "[mm]"¶ endif¶ if "[hh]" "<" "10"¶ setvar "[th]" "0[hh]"¶ else¶ setvar "[th]" "[hh]"¶ endif¶endif

++++++++++++++
From Costa Rica
Pura Vida
User avatar
virger
 
Posts: 510
Joined: Mon Sep 18, 2006 12:21 pm
Location: Costa Rica, America Central

Postby RickOShay » Mon Apr 30, 2012 1:41 am

Gaev wrote:RickOShay:

1) [P1TimeE] is returned in milliseconds ... not seconds ... so you have to divide it by 1000 first in order to get the number of elapsed seconds.


Hi Gaev thanks for helping.
Sorry I should have been clearer - [P1TimeE] is a simple incremental counter run every 1000ms [P1TimeE] increments by 1. i.e. it's not the Timer variable to store elapsed ms.

2) What is the reason for the first command ... where you add 1 to [P1TimeE] ?


As above each second [P1TimeE] increases by 1.

3) Why don't you post your results here ... start with [P1TimeE] and [P1TimeElapsed] ... and if that does not give us a clue, may have to ask for the other variables as well.

Also, NeoBook would balk at this line ...
Code: Select all
[b].add extra zero in display[/b]


This was an error in the post I tried to Bold some of the code = didn't work obviously :)
Last edited by RickOShay on Mon Apr 30, 2012 3:43 am, edited 2 times in total.
There's no place like 127.0.0.1

http://www.farenheit451.net
User avatar
RickOShay
 
Posts: 22
Joined: Sun Jul 27, 2008 5:59 am
Location: Cape Town, SA

Re: Try...

Postby RickOShay » Mon Apr 30, 2012 2:01 am

virger wrote:
Code: Select all
{NeoBook 5 Objects}

Code.......


++++++++++++++
From Costa Rica
Pura Vida


Thanks very much Virger (and Gaev) for quick replies.

Interestingly in Neobook 5.7.1 when running your code above I get Error Error Error output for [hh] [mm] [ss] - exactly the same result I get with the 5 different timers I've written over the years and that work with previous versions of Neobook. Have you tried running this with 5.7.1?

I even downloaded latest ver of NB again and reinstalled. Still same problem.

<ten minutes later>

I just tried a very simple Math function and get and error in [mins] immediately:

Math "Trunc ([secs] / 60)" "-1" "[mins]"

In the above I have a timer that increments [secs] by one every 1000ms.
Sorry I'm starting to wonder if its me or my machine. ':oops:' Strange - I don't get any funnies in Excel calculations.

<even later>

I just uninstalled NB 5.7.1 and installed NB 5.62 and ran the same pubs without error. So something definitely wrong with Math calc in 5.7.1 unless there are changes to syntax which are not documented.

So this routine does work perfectly if you stick it on a timer and have a text object displaying: [P1Hrs]:[P1Mins]:[P1Secs]

Thanks goodness! I thought I was losing it there for a minute! ':o'

Code: Select all
Math "trunc ([P1TimeE]+1)" "-1" "[P1TimeE]"

. convert elapsed time into hours, minutes & seconds for display

. take total elapsed seconds and divide by # secs in an hour to get hrs
Math "trunc([P1TimeE]/3600)" "-1" "[P1Hrs]"
Math "trunc([P1Hrs]*3600)" "-1" "[P1secsinHrs]"

.take seconds in elapsed hours and subtract from total elapsed time
Math "trunc([P1TimeE]-[P1secsinHrs])" "-1" "[P1TimeRem]"

.take remainder and divide by secs in hour to get mins
Math "trunc([P1TimeRem]/60)" "-1" "[P1Mins]"
Math "trunc([P1Mins]*60)" "-1" "[P1secsinMins]"

.get remainder secs
Math "trunc([P1TimeRem]-[P1secsinMins])" "-1" "[P1Secs]"

.change display in case secs = 60
If "[P1Secs]" "=" "60"
Setvar "[P1Mins]" "1+[P1Mins]"
Endif

.add extra zero in display
If "[P1Secs]" "<" "10"
Setvar "[P1Secs]" "0[P1secs]"
Endif

. change mins and hrs in case mins = 1 hr
If "[P1Mins]" "=" "60"
Setvar "[P1Mins]" "00"
Setvar "[P1Hrs]" "1+[P1Hrs]"
Endif

.add extra zero in display
If "[P1Mins]" "<" "10"
Setvar "[P1Mins]" "0[P1mins]"
Endif

.add extra zero in display
If "[P1Hrs]" "=" "0"
Setvar "[P1Hrs]" "00"
Endif

.output string
SetVar "[P1TimeElapsed]" "[P1Hrs]:[P1Mins]:[P1Secs]"
There's no place like 127.0.0.1

http://www.farenheit451.net
User avatar
RickOShay
 
Posts: 22
Joined: Sun Jul 27, 2008 5:59 am
Location: Cape Town, SA

Postby Gaev » Mon Apr 30, 2012 6:43 am

RickOShay:

I don't have v5.7.1 installed ... so I can not say what might be wrong ... but it worked just fine on my older version.

Using v5.7.1, call the subroutine from a button (after you setup the variable [P1TimeE] with some static value ... say 100 and then 1000) ; then look in the debugger for the values of these associated variables ...

[P1TimeE]
[P1Hrs]
[P1secsinHrs]
[P1TimeRem]
[P1Mins]
[P1secsinMins]
[P1Secs]
[PiTimeElapsed]

... and post them here ... so I can compare with my experience and determine where the difference lies.
User avatar
Gaev
 
Posts: 3718
Joined: Fri Apr 01, 2005 7:48 am
Location: Toronto, Canada

Postby RickOShay » Mon Apr 30, 2012 7:20 am

Gaev wrote:RickOShay:

I don't have v5.7.1 installed ... so I can not say what might be wrong ... but it worked just fine on my older version.

Using v5.7.1, call the subroutine from a button (after you setup the variable [P1TimeE] with some static value ... say 100 and then 1000) ; then look in the debugger for the values of these associated variables ...

[P1TimeE]
[P1Hrs]
[P1secsinHrs]
[P1TimeRem]
[P1Mins]
[P1secsinMins]
[P1Secs]
[PiTimeElapsed]

... and post them here ... so I can compare with my experience and determine where the difference lies.


Thanks very much Gaev

I've already checked the debug info. Debug shows "Error" in all variables except [P1TimeE] in 5.7.1 as soon as the routine runs - so no help there.

I'm not really new to Neobook - I've built lots of applications including a fully functional Chess program "Chesster" etc. http://www.neosoftware.com/forum/viewtopic.php?t=15567&highlight=chess

As mentioned above I tested the same routine with a previous version of NB 5.6.2 and it worked fine.

It clearly has something to do with the Math function in 5.7.1. Apparently changes were made to fix a database issue.

Seems these fixes broke something else.

BTW what version are you using?
There's no place like 127.0.0.1

http://www.farenheit451.net
User avatar
RickOShay
 
Posts: 22
Joined: Sun Jul 27, 2008 5:59 am
Location: Cape Town, SA

Postby Gaev » Mon Apr 30, 2012 10:44 am

RickOShay:
It clearly has something to do with the Math function in 5.7.1. Apparently changes were made to fix a database issue.
It was to allow the Math command to accept (continental) European number formats e.g. US/UK 1,234.56 is formated as 1.234,56

So try and replace the -1 in the second parameter of each of your Math commands with 0 ... after all, when you truncate those values, you want to end up with an integer value anyways.
BTW what version are you using?
v5.6.4a (August 2009)
User avatar
Gaev
 
Posts: 3718
Joined: Fri Apr 01, 2005 7:48 am
Location: Toronto, Canada

Postby Neosoft Support » Mon Apr 30, 2012 11:26 am

Rick:

I tried inserting your sample code into a timer object and it seemed to work correctly with v5.7.1. The [P1ElaspedTime] variable appeared to update correctly and showed the elapsed time which appeared to be accurate. However, I have my regional control panel setting configured for "English (United States)". I noticed on the forum that you list your location as "South Africa". When I change my region to "English (South Africa)" I get the same errors that you do.

I'm not sure what it is about South Africa's region settings that are confusing the Math action, but it will probably require an update to correct. In the mean time, the following modified code will work with v5.7.1 and the South Africa region settings:

Code: Select all
Math "[P1TimeE]+1" "0" "[P1TimeE]"

. convert elapsed time into hours, minutes & seconds for display

. take total elapsed seconds and divide by # secs in an hour to get hrs
SetVar "[P1Hrs]" "trunc([P1TimeE]/3600)"
SetVar "[P1secsinHrs]" "trunc([P1Hrs]*3600)"

.take seconds in elapsed hours and subtract from total elapsed time
SetVar "[P1TimeRem]" "trunc([P1TimeE]-[P1secsinHrs])"

.take remainder and divide by secs in hour to get mins
SetVar "[P1Mins]" "trunc([P1TimeRem]/60)"
SetVar "[P1secsinMins]" "trunc([P1Mins]*60)"

.get remainder secs
SetVar "[P1Secs]" "trunc([P1TimeRem]-[P1secsinMins])"

.change display in case secs = 60
If "[P1Secs]" "=" "60"
Setvar "[P1Mins]" "1+[P1Mins]"
Endif

.add extra zero in display
If "[P1Secs]" "<" "10"
Setvar "[P1Secs]" "0[P1secs]"
Endif

. change mins and hrs in case mins = 1 hr
If "[P1Mins]" "=" "60"
Setvar "[P1Mins]" "00"
Setvar "[P1Hrs]" "1+[P1Hrs]"
Endif

.add extra zero in display
If "[P1Mins]" "<" "10"
Setvar "[P1Mins]" "0[P1mins]"
Endif

.add extra zero in display
If "[P1Hrs]" "=" "0"
Setvar "[P1Hrs]" "00"
Endif

.output string
SetVar "[P1TimeElapsed]" "[P1Hrs]:[P1Mins]:[P1Secs]"


The SetVar action can also do math, but it doesn't perform the extra character processing that the Math action does.
NeoSoft Support
Neosoft Support
NeoSoft Team
 
Posts: 5593
Joined: Thu Mar 31, 2005 10:48 pm
Location: Oregon, USA

Postby RickOShay » Tue May 01, 2012 4:34 am

Neosoft Support wrote:Rick:

I tried inserting your sample code into a timer object and it seemed to work correctly with v5.7.1. The [P1ElaspedTime] variable appeared to update correctly and showed the elapsed time which appeared to be accurate. However, I have my regional control panel setting configured for "English (United States)". I noticed on the forum that you list your location as "South Africa". When I change my region to "English (South Africa)" I get the same errors that you do.

I'm not sure what it is about South Africa's region settings that are confusing the Math action, but it will probably require an update to correct. In the mean time, the following modified code will work with v5.7.1 and the South Africa region settings:

Code: Select all
Math "[P1TimeE]+1" "0" "[P1TimeE]"
.....


The SetVar action can also do math, but it doesn't perform the extra character processing that the Math action does.


Excellent thanks.

It seems to be related to Trunc - perhaps it searches for delimiter from left to right and so includes a "," in the numeric value?

The regional settings for SA are:
Decimal symbol = "."
List separator = ","
Digit grouping symbol = Spaces are used between 000 and not ","

Countries this will affect : http://en.wikipedia.org/wiki/File:DecimalSeparator.svg

The whole of Europe (excl UK) Parts of Asia (excl China,India, central Asia) SE Asia, S.America, Greenland, S.Africa, Cameroon, Quebec.

Thanks very much for the quick help - it's much appreciated.

Regards

Mike Goode
(RickOShay)
There's no place like 127.0.0.1

http://www.farenheit451.net
User avatar
RickOShay
 
Posts: 22
Joined: Sun Jul 27, 2008 5:59 am
Location: Cape Town, SA

Postby Neosoft Support » Tue May 01, 2012 11:18 am

It's not the decimal separator, I think it's the digit grouping symbol. I believe that the Math function works correctly in Europe and Asia.
NeoSoft Support
Neosoft Support
NeoSoft Team
 
Posts: 5593
Joined: Thu Mar 31, 2005 10:48 pm
Location: Oregon, USA


Return to NeoBook Action Commands

Who is online

Users browsing this forum: No registered users and 2 guests

cron