Sorry your browser is not supported!

You are using an outdated browser that does not support modern web technologies, in order to use this site please update to a new browser.

Browsers supported include Chrome, FireFox, Safari, Opera, Internet Explorer 10+ or Microsoft Edge.

DarkBASIC Professional Discussion / Timer() command acting wierd

Author
Message
DB PROgrammer
17
Years of Service
User Offline
Joined: 9th Feb 2007
Location: Nowhere But Everywhere
Posted: 10th Mar 2008 04:22 Edited at: 10th Mar 2008 04:25
Well I want on one of my projects today and ran it but it wouldn't shoot, it was because I was using the timer() command for timer based shooting. The problem is the timer() command was returning negitve values Now I know I can fix this by using ABS() but i'm wondering why its happening. BTW it is increasing(getting smaller sense it in negetive)

Like I said it's not really a problem just wonder why it's happening.

Edit:
Just tried using ABS() and it dosn't work because the number is still decreasing. Any help? I'm going to try restarting the computer sense i'm pretty sure that resets the clock.


I'm Pro grammer.
Codger
21
Years of Service
User Offline
Joined: 23rd Nov 2002
Location:
Posted: 10th Mar 2008 04:34
I just checked and mine timer is increasing

would you post some code?

System
PIV 2.8 MZ 512 Mem
FX 5600 256 mem
DB PROgrammer
17
Years of Service
User Offline
Joined: 9th Feb 2007
Location: Nowhere But Everywhere
Posted: 10th Mar 2008 07:18
The code was just "text 0,0,""+str$(timer())", what I ment by decreasing was sense the timer was negetive and it wsa inc'ing the number got smaller so when I used abs() it was dec'ing. But restarting the computer worked.


I'm Pro grammer.
DB PROgrammer
17
Years of Service
User Offline
Joined: 9th Feb 2007
Location: Nowhere But Everywhere
Posted: 10th Mar 2008 20:31
Quote: "Was the system running for more 24 days?"


Maybe, it's not my computer so I don't really know.


I'm Pro grammer.
Green Gandalf
VIP Member
19
Years of Service
User Offline
Joined: 3rd Jan 2005
Playing: Malevolence:Sword of Ahkranox, Skyrim, Civ6.
Posted: 10th Mar 2008 22:37
Interesting point.

Lots of shaders, among other things, use system based timers. If these start from zero when a PC is switched on then you are probably safe unless you keep a machine switched on continually (which you probably shouldn't in these environmentally conscious days). But if you really must keep your machine switched on long enough for the timer to "flip over" (or really have no idea how long its been switched on) then you need to code for it. Simple as that.

Of course, a simple solution is to have a grandchild around. It took me two weeks to realise that the reason my screensaver wasn't working correctly was that my grandson crawled along the floor and switched off my life support machine at the socket. Such an act would have solved your problem too.

Quote: "but DBPro's Timer() command wrongly returns a signed integer instead of a dword, so it runs into negative values after half of the time"


Hardly matters, surely? Most of the time timer() is used to calculate a difference, i.e. time elapsed. As long as you know what you are doing you should be OK. But why "wrongly" anyway? Here's a Help file snippet:

Quote: "Return Integer=TIMER()"


What's wrong with that? Seems clear and unambiguous to me. Or are trying to say it doesn't return an integer?
DB PROgrammer
17
Years of Service
User Offline
Joined: 9th Feb 2007
Location: Nowhere But Everywhere
Posted: 10th Mar 2008 22:46
Quote: "then you need to code for it."


That's what I was thinking, I don't think many of us DBPro coders(I didn't) even know it's posible and therfore never code for it.

Quote: "Of course, a simple solution is to have a grandchild around."


In forty years that might just work


I'm Pro grammer.
RiiDii
19
Years of Service
User Offline
Joined: 20th Jan 2005
Location: Inatincan
Posted: 10th Mar 2008 23:15
"Return Integer=TIMER()"

While that is what the help file says, it should be:

"Return DWord=TIMER()"

The problem is the conversion of a DWord to an Integer; when the sign-bit is set to 1 (or 0, I have no idea which way it is), the Integer becomes negative.

Change this:

text 0,0,""+str$(timer())

to

myTimer as Dword
myTimer=timer()
text 0,0,""+str$(myTimer)


To fix the problem of a resetting timer() (one that has been on too long and starts back at 0), simply calculate the difference in time, and if it is negative, set it to some default expected value. Example:

DeltaTime=Timer()-LastTimer
If DeltaTime<0 Then DeltaTime=133


The one frame in which this occurs will not be noticeable by anyone.


Open MMORPG: It's your game!
Green Gandalf
VIP Member
19
Years of Service
User Offline
Joined: 3rd Jan 2005
Playing: Malevolence:Sword of Ahkranox, Skyrim, Civ6.
Posted: 10th Mar 2008 23:48
Quote: "and if it is negative, set it to some default expected value"


Why not set it to the correct value? You know how much it has been changed by (because you know the values of dwords/integers at which the change happens) so it's simple arithmetic to work it out.

Unless you've had your PC on so long that timer() has flipped over more than once ...

Quote: "The one frame in which this occurs will not be noticeable by anyone."


I'd notice it on my machine if I was using some of the shader demos available on this site.

Quote: "The problem is the conversion of a DWord to an Integer"


Why is that a problem?
RiiDii
19
Years of Service
User Offline
Joined: 20th Jan 2005
Location: Inatincan
Posted: 11th Mar 2008 06:40
Quote: "I'd notice it on my machine if I was using some of the shader demos available on this site."


Wow! Less than 1/30th of second every 24 hours and you would notice. Impressive!

Fun aside GG, I would guess that it is far better to have an estimated 33 (1/30th of a second) for that one frame than to have roughly a -2^32 for that one frame, which would probably be far more noticeable.

Quote: "Why not set it to the correct value? You know how much it has been changed by (because you know the values of dwords/integers at which the change happens) so it's simple arithmetic to work it out."


A good solution if that 1/30th of second accuracy is required every 24 hours. I do agree that it would be easy to calculate, so - hey, why not?

Quote: "Why is that a problem?"


Not problem in most cases. But, as in DB PROgrammer's situation where he is trying to look at it and understand it, posts a thread about it... well, still not an actual "problem" I suppose, but certainly a perceived one.


Open MMORPG: It's your game!
dark coder
21
Years of Service
User Offline
Joined: 6th Oct 2002
Location: Japan
Posted: 11th Mar 2008 07:54
Quote: "A good solution if that 1/30th of second accuracy is required every 24 hours. I do agree that it would be easy to calculate, so - hey, why not?"


GG is saying calculating the elapsed time even when the int/DWord limit is reached is very easy, which is true, you just add the current timer() value to the elapsed time between the last timer() value and the int/DWord limit, if it's this easy then why use a hack and specify a literal of 1/30th of a second?

RiiDii
19
Years of Service
User Offline
Joined: 20th Jan 2005
Location: Inatincan
Posted: 11th Mar 2008 12:19
Which is why I agreed with GG.


Open MMORPG: It's your game!
TinTin
17
Years of Service
User Offline
Joined: 16th May 2006
Location: BORG Drone Ship - Being Assimilated near Roda Beta (28)
Posted: 11th Mar 2008 12:58
I wonder if this would be a fix....

Just for the record...
An Integer has the range, -2,147,483,648 to +2,147,483,647
A dWord has the range, 0 to +4,294,967,295

Cyberspace was becoming overcrowded and slummy so I decided to move. These nice chaps gave me a lift.
DB PROgrammer
17
Years of Service
User Offline
Joined: 9th Feb 2007
Location: Nowhere But Everywhere
Posted: 11th Mar 2008 14:37 Edited at: 11th Mar 2008 14:37
Thanks for explaining it guys. But I thought of somthing, even if it's a dword just because it can't go negetive dosn't mean there isn't going to be a problem because what if you made for example waittime=timer()+100 at +4,294,967,290 and then it would switch over to 0 then the check If timer()>waittime wouldn't ever be true... ...wait you couldn't add 100 to waitime because it would be over +4,294,967,295 so it would switch around back to zero and add the remaining 95, so it would still work right?


I'm Pro grammer.
TinTin
17
Years of Service
User Offline
Joined: 16th May 2006
Location: BORG Drone Ship - Being Assimilated near Roda Beta (28)
Posted: 11th Mar 2008 14:44
That would be worthy of a little test....
I'm sure that it would be true though.

Cyberspace was becoming overcrowded and slummy so I decided to move. These nice chaps gave me a lift.
DB PROgrammer
17
Years of Service
User Offline
Joined: 9th Feb 2007
Location: Nowhere But Everywhere
Posted: 11th Mar 2008 16:24
Here I did a "little test"



So it's true(other then the fact that it's 94 because when I said 95 I didn't take into account that it would count 0)


I'm Pro grammer.
Dewi Morgan
16
Years of Service
User Offline
Joined: 16th Jan 2008
Location: London, UK
Posted: 11th Mar 2008 18:39
Here's what I have, then.


Would appreciate if people could verify that the "if TickLength < 0 then TickLength = (4294967295-last_tick)+time_now" line looks right, if both time_now and TickLength are declared as Dwords. Using a number as big as that in an expression makes me leery: what if all maths is automatically converted to ints?

Yet another programmer - http://www.thudgame.com/mmo
All my posts are Public Domain unless I say otherwise.
You may use all my code and ideas as you see fit.
Ian T
21
Years of Service
User Offline
Joined: 12th Sep 2002
Location: Around
Posted: 11th Mar 2008 19:15
Is IanM's HiTimer() affected by this as well? I can't think of any way to test off the top of my head other than leaving my PC on for 24 days
Dewi Morgan
16
Years of Service
User Offline
Joined: 16th Jan 2008
Location: London, UK
Posted: 11th Mar 2008 19:31
I was wondering the same thing. It depends what it uses internally, and what you're reading it into. Obviously, whatever variable you read it into, WILL be affected, whether DWord or Int.

If it uses Ints or Dwords internally, then you might get the problem even if you're calling it as HiTimer(1). If it uses floats, then that'd be even worse: it'd lose resolution over time.

A question for IanM, I think?

Yet another programmer - http://www.thudgame.com/mmo
All my posts are Public Domain unless I say otherwise.
You may use all my code and ideas as you see fit.
TinTin
17
Years of Service
User Offline
Joined: 16th May 2006
Location: BORG Drone Ship - Being Assimilated near Roda Beta (28)
Posted: 11th Mar 2008 20:58
Here is a lovely wrapper...


Cyberspace was becoming overcrowded and slummy so I decided to move. These nice chaps gave me a lift.
Benjamin
21
Years of Service
User Offline
Joined: 24th Nov 2002
Location: France
Posted: 12th Mar 2008 02:52 Edited at: 12th Mar 2008 02:53
Quote: "Here is a lovely wrapper..."

For what? There isn't always much use for outputting the result of timer to the screen.

Quote: "Is IanM's HiTimer() affected by this as well?"

Yes, as eventually it has to roll over, like any other counter.

TinTin
17
Years of Service
User Offline
Joined: 16th May 2006
Location: BORG Drone Ship - Being Assimilated near Roda Beta (28)
Posted: 12th Mar 2008 11:17
@Benjamin
Quote: "For what? "

Well, if you look at the code, you'll see it doesn't put anything to the screen, thats just the sample code to show it working.
The toDword() function is the wrapper of sorts, it basicaly takes any value as a parameter and converts it to a dWord, Similar functions can be created for most of the other numerical types.

As with all programming languages and basic PC archetecture, all numbers loop when they reach their limits this is due to the way binary numbers work...

00 = 0
01 = 1
10 = 2
11 = 3
next in the 2-bit sequence would be
00 = 0

Every numerical value (even floating point) are used/stored this way so a 16-bit integer and a 16-bit dWord would have the same amount of bits but since the integer supports negative numbers it's max value is only half the value of a dWord, floats, doubles, shorts, uints all do the same.

It would be handy if DBP supported the #define command it's self (I know there is a plugin that does!) or even a series of numerical converter functions.


Cyberspace was becoming overcrowded and slummy so I decided to move. These nice chaps gave me a lift.
Benjamin
21
Years of Service
User Offline
Joined: 24th Nov 2002
Location: France
Posted: 12th Mar 2008 15:08
Quote: "it basicaly takes any value as a parameter and converts it to a dWord, Similar functions can be created for most of the other numerical types"

But why use that instead of just using a dword variable?

TinTin
17
Years of Service
User Offline
Joined: 16th May 2006
Location: BORG Drone Ship - Being Assimilated near Roda Beta (28)
Posted: 12th Mar 2008 16:20 Edited at: 12th Mar 2008 16:21
well Benjamin, not all of the native DBP commands return values in the format you want. e.g. the Timer() returns a signed integer... which was the posts original issue.
and you can do the following...

For A=0 to 180
Print A," ",Cos(toFloat(A/180))," ",Cos(A/180)
Next
Wait Key
End
Function toFloat(I as float):EndFunction I


In C++ there are standard function that do this, so I guess there is a need.

Cyberspace was becoming overcrowded and slummy so I decided to move. These nice chaps gave me a lift.
Benjamin
21
Years of Service
User Offline
Joined: 24th Nov 2002
Location: France
Posted: 12th Mar 2008 16:22
Quote: "... which was the posts original issue."

I don't see an issue.



TinTin
17
Years of Service
User Offline
Joined: 16th May 2006
Location: BORG Drone Ship - Being Assimilated near Roda Beta (28)
Posted: 12th Mar 2008 17:53 Edited at: 12th Mar 2008 17:57
@Benjamin
Quote: "
The problem is the timer() command was returning negitve values
"
that issue, the DBP Timer() function returns an unsigned integer value instead of a dWord, hence the negative value.

In Riidii's example... (several posts further on) you see the solution as..

myTimer as Dword
myTimer=timer()
text 0,0,""+str$(myTimer)

instead my wrapper function could have been used thus...

text 0,0,Str$(toDword(Timer())

which, in my oppinion, is a lot easier to read...

*note: There is nothing wrong with Riidii's post, my post was just an alterative method.


Cyberspace was becoming overcrowded and slummy so I decided to move. These nice chaps gave me a lift.
Benjamin
21
Years of Service
User Offline
Joined: 24th Nov 2002
Location: France
Posted: 12th Mar 2008 17:56
Quote: "instead my wrapper function could have been used thus...

text 0,0,Str$(toDword(Timer())"

Which again brings me to the question, what's the point? You rarely need to output the result of timer() to the screen.

TinTin
17
Years of Service
User Offline
Joined: 16th May 2006
Location: BORG Drone Ship - Being Assimilated near Roda Beta (28)
Posted: 12th Mar 2008 18:07 Edited at: 12th Mar 2008 23:07
Doh!, you are talking the pi$$? aren't you?
Do you ask why GOD didn't just put a flap on our back to allow the used air in our lungs to escape, allowing us to just breath in?

The point of displaying it onto the screen is for demonstration purposes only, i.e. this isn't the method you'd normaly use the timer. WindowsKiller last post details the problem also, this being that DarkBasicPro returns an signed integer for the timer when it should be a dword. This isn't realy a problem until you use the timer() on an application thats been running for over 24 days. Then you'd get negative timer() values.

A Timer() value of 2,147,483,647 would be -2,147,483,644 just 5 milliseconds later, and calculating the difference would be more complex than doing it with dword's.


Cyberspace was becoming overcrowded and slummy so I decided to move. These nice chaps gave me a lift.
Benjamin
21
Years of Service
User Offline
Joined: 24th Nov 2002
Location: France
Posted: 12th Mar 2008 18:08 Edited at: 12th Mar 2008 18:19
Quote: "WindowsKiller last post details the problem also, this being that DarkBasicPro returns an unsigned integer for the timer when it should be a dword. "

Why does this matter? If you're finding the difference between the current timer value and the last, whether the variable is a dword or signed integer makes no difference at all.

Quote: "A Timer() value of 2,147,483,647 would be -2,147,483,644 just 5 milliseconds later"

Good, because -2,147,483,644 - 2,147,483,647 is 5 when working with 32-bit variables. You don't need any extra code to calculate the difference.

dark coder
21
Years of Service
User Offline
Joined: 6th Oct 2002
Location: Japan
Posted: 12th Mar 2008 18:13
Quote: "this being that DarkBasicPro returns an unsigned integer for the timer when it should be a dword"


You do realise an unsigned integer is a DWord right?

Green Gandalf
VIP Member
19
Years of Service
User Offline
Joined: 3rd Jan 2005
Playing: Malevolence:Sword of Ahkranox, Skyrim, Civ6.
Posted: 12th Mar 2008 21:03 Edited at: 13th Mar 2008 12:53
@WindowsKiller [Edit: Addressee added for TinTin's benefit. ]

Quote: "as the timer can not be negative."


Of course it can if it's cast as a signed integer. I repeat: "What's the problem?". You just code in the appropriate way for whatever type you've been given. The Help file says it's returned as an integer. No-one has given any evidence to suggest that is false. So, again, what's the problem?

Quote: "it will run into negative values after 24 days"


Very true. So what?

(I guess it will then take another 48 days before it trips again - but I have no intention of sitting here to check. )

Interesting what controversy an innocent question has provoked.
DB PROgrammer
17
Years of Service
User Offline
Joined: 9th Feb 2007
Location: Nowhere But Everywhere
Posted: 12th Mar 2008 21:28
Quote: "Interesting what controversy an innocent question has provoked."


Oops...


I'm Pro grammer.
TinTin
17
Years of Service
User Offline
Joined: 16th May 2006
Location: BORG Drone Ship - Being Assimilated near Roda Beta (28)
Posted: 12th Mar 2008 23:06 Edited at: 12th Mar 2008 23:16
oop's my bag, I should be using Signed instead of Unsigned in my previous post, I go fix that. Well Spotted Dark Coder. hehe

I don't hve any problem @GG, I just responded to the original post with a function that converts signed integers into dWords... whats the problem with that? Ask Benjamin...

Cyberspace was becoming overcrowded and slummy so I decided to move. These nice chaps gave me a lift.
DB PROgrammer
17
Years of Service
User Offline
Joined: 9th Feb 2007
Location: Nowhere But Everywhere
Posted: 12th Mar 2008 23:30
Quote: "oop's my bag"


Just though that was kinda funny.


I'm Pro grammer.
Benjamin
21
Years of Service
User Offline
Joined: 24th Nov 2002
Location: France
Posted: 13th Mar 2008 03:18
Quote: "I just responded to the original post with a function that converts signed integers into dWords... whats the problem with that? Ask Benjamin..."

I was merely point out that it isn't necessary.

Green Gandalf
VIP Member
19
Years of Service
User Offline
Joined: 3rd Jan 2005
Playing: Malevolence:Sword of Ahkranox, Skyrim, Civ6.
Posted: 13th Mar 2008 12:51
@TinTin

Quote: "I don't hve any problem @GG, I just responded to the original post with a function that converts signed integers into dWords... whats the problem with that?"


I know you don't - I was talking to WindowsKiller (and I was quoting from his post!).

[Edited previous post just in case other sensitive souls think I was quoting from them. There are some odd people on this forum... ]
Cash Curtis II
19
Years of Service
User Offline
Joined: 8th Apr 2005
Location: Corpus Christi Texas
Posted: 13th Mar 2008 12:56
Quote: "Why does this matter? If you're finding the difference between the current timer value and the last, whether the variable is a dword or signed integer makes no difference at all."

This is completely true. This is a non issue. The difference of time between the current and last call to the timer is all that matters.

Outputting the timer is a non issue too, because printing the timer() return value is relatively useless for timer based code. All that is useful is the difference between the two calls, which will always be positive. I've had my computer running for more than 24 days but this never caused an issue for me and I only use timer based movement and animation.


Come see the WIP!
Green Gandalf
VIP Member
19
Years of Service
User Offline
Joined: 3rd Jan 2005
Playing: Malevolence:Sword of Ahkranox, Skyrim, Civ6.
Posted: 13th Mar 2008 14:41
Quote: "which will always be positive"


That's the point. There IS a brief instant when the timer flips over to the start so the difference IS negative at that instant (which everyone else seems agreed on ). Whether or not that matters depends entirely on how you use the time difference on the very rare occasions that it happens. If you are using it to move objects around they could instantaneously move an enormous distance in the wrong direction!

I've never experienced it certainly - but it can theoretically happen.

Quote: "This is a non issue."


Most of the time.
TinTin
17
Years of Service
User Offline
Joined: 16th May 2006
Location: BORG Drone Ship - Being Assimilated near Roda Beta (28)
Posted: 13th Mar 2008 14:42 Edited at: 13th Mar 2008 14:51
Thats much better GG Yup there are loads of odd sensitive folk on here... ( me and you especialy hehe)

@Benjamin, I know what your saying, although I find it rather negative. If there is no point in doing this conversion, then why is there a standard group of commands in C++ that do just the same? Think! what it'd look like if it were binary numbers...

@Crash, Agreed!, but DB asked for whatever reason, I think he deserved a decent solution... call me a saddo' (don't go there!)

Hope you got your answer DB?

Go Create...

Cyberspace was becoming overcrowded and slummy so I decided to move. These nice chaps gave me a lift.
Benjamin
21
Years of Service
User Offline
Joined: 24th Nov 2002
Location: France
Posted: 13th Mar 2008 15:00 Edited at: 13th Mar 2008 15:27
Quote: "then why is there a standard group of commands in C++ that do just the same?"

[Edit] I see what you're saying now. You don't need to convert at all, that's the point I'm making. Besides, you don't ever actually convert between signed and unsigned.

Quote: "There IS a brief instant when the timer flips over to the start so the difference IS negative at that instant"

There is? I don't see how this can happen.



DB PROgrammer
17
Years of Service
User Offline
Joined: 9th Feb 2007
Location: Nowhere But Everywhere
Posted: 13th Mar 2008 15:16
Quote: "Hope you got your answer DB?"


Oh yes, that was answer quiet a while ago


I'm Pro grammer.
TinTin
17
Years of Service
User Offline
Joined: 16th May 2006
Location: BORG Drone Ship - Being Assimilated near Roda Beta (28)
Posted: 13th Mar 2008 15:20
We could discuss this forever... it's sort of like what came first, the Chicken or the Egg?

Cyberspace was becoming overcrowded and slummy so I decided to move. These nice chaps gave me a lift.
Benjamin
21
Years of Service
User Offline
Joined: 24th Nov 2002
Location: France
Posted: 13th Mar 2008 15:43
Quote: "We could discuss this forever... "

So you still defend that 'casting' to a dword is necessary?

TinTin
17
Years of Service
User Offline
Joined: 16th May 2006
Location: BORG Drone Ship - Being Assimilated near Roda Beta (28)
Posted: 13th Mar 2008 16:11 Edited at: 13th Mar 2008 16:12
very dry... hehe
or are you just trying to push this forum past the 50 limit...?

Cyberspace was becoming overcrowded and slummy so I decided to move. These nice chaps gave me a lift.
Benjamin
21
Years of Service
User Offline
Joined: 24th Nov 2002
Location: France
Posted: 13th Mar 2008 16:14
Am I missing something?

jinzai
17
Years of Service
User Offline
Joined: 19th Aug 2006
Location: USA
Posted: 13th Mar 2008 16:17 Edited at: 13th Mar 2008 16:19
I think that he is wary of it.

The difference is between casting and promotion of variables. Casting changes how a variable is perceived, and does not change the variable itself. Promotion without casting will change a variable. If for example, you make a DBPro WORD value (that represents a short int) an integer, you will lose the sign. That is promotion without casting.

This particular item has been hashed out by the experts, who are ...btw, just as unclear about it as is apparent here. Yes, you can safely use the value without making it a DWORD, but...imo it is a better practice to use a DWORD because that is what it is in the original function.

After that, it is just semantics, which can indeed be argued ad infinitum et nauseum.
Benjamin
21
Years of Service
User Offline
Joined: 24th Nov 2002
Location: France
Posted: 13th Mar 2008 16:30
Quote: "The difference is between casting and promotion of variables. Casting changes how a variable is perceived, and does not change the variable itself. Promotion without casting will change a variable. If for example, you make a DBPro WORD value (that represents a short int) an integer, you will lose the sign. That is promotion without casting."

Interesting, I hadn't heard the term promotion until this point.

Not to nitpick, but DBP's WORD type is actually an unsigned short int. I understand what you're saying though.

Green Gandalf
VIP Member
19
Years of Service
User Offline
Joined: 3rd Jan 2005
Playing: Malevolence:Sword of Ahkranox, Skyrim, Civ6.
Posted: 13th Mar 2008 20:21
@Benjamin

Quote: "Quote: "There IS a brief instant when the timer flips over to the start so the difference IS negative at that instant"
There is? I don't see how this can happen.

+ Code Snippet
a = -100 ` 100 before the flip over
b = 5 ` 5 after the flip over

print b - a
wait key"


Pardon? There's no "flip over" in your example.

You've got it completely the wrong way round!

The "flip over" referred to is the enormous change in value you get when the counter reaches its maximum possible value and then in the next instant "overflows" to its smallest possible value, so the change has to be negative,

i.e. before "flip over", timer = a very large positive number;

after "flip over", timer = a very large negative number (if signed integers are used) or a small positive number (if dwords are used).

In either case, the difference, i.e. "b-a" in your example, is large and negative.

Quote: "Am I missing something?"


Looks like it.
dark coder
21
Years of Service
User Offline
Joined: 6th Oct 2002
Location: Japan
Posted: 13th Mar 2008 20:46
Benjamin is correct, but his example isn't. Before the 'flip' i.e. when the 32nd bit is flipped in a signed int, the integer value will go from 2,147,483,647 to -2,147,483,648, if you then do what benjamin said, b - a, or -2,147,483,648 - 2,147,483,647, assuming more than 1 ms elapses, the integer value will overflow and become the correct elapsed time, it's only when using a DWord do you require an additional check for the flip.

Benjamin
21
Years of Service
User Offline
Joined: 24th Nov 2002
Location: France
Posted: 13th Mar 2008 22:05 Edited at: 13th Mar 2008 22:38
Quote: "Pardon? There's no "flip over" in your example."

You said flips over to the start, which I assume means flipping over to the first value (ie. 0). Otherwise, this demonstrates it when the 32nd bit flips:



Quote: "That's a non-issue? "

Well I sure don't see an issue:



I'm going to pre-empt someone telling me I'm counting backwards, and show what happens when you reach the 32nd bit:



Quote: "The "flip over" referred to is the enormous change in value you get when the counter reaches its maximum possible value and then in the next instant "overflows" to its smallest possible value, so the change has to be negative"

Correct the change is negative. However, it's such a big change that it cannot fit in a 32-bit variable and thus an underflow occurs, yielding a positive value.

If you had have done some testing, you would have found this the case:



The only time a negative value can result is if the difference is higher than (2^31)-1.

Cash Curtis II
19
Years of Service
User Offline
Joined: 8th Apr 2005
Location: Corpus Christi Texas
Posted: 13th Mar 2008 23:20 Edited at: 13th Mar 2008 23:29
Quote: "Wrong. Once that the timer runs into negative values, it will count backwards."

No, you're wrong. The number will continue to increment, the result is that the absolute value of the number will continue to decrease but since it's negative it's actually increasing. That's a pretty basic mathematical concept.

When you subtract a small negative number from a larger negative number the result will be positive. Example...

Quote: "-1-(-100)
-1+100
result=99"



Come see the WIP!

Login to post a reply

Server time is: 2024-05-03 20:33:35
Your offset time is: 2024-05-03 20:33:35