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 / Strange calc error

Author
Message
Fane
14
Years of Service
User Offline
Joined: 9th May 2012
Location:
Posted: 28th Aug 2014 23:00
Hello everybody its me again
I have here a strange calculation error.



vel# should be always 20 not matter how many frames I get. It works with 60 frames and above but at 45 frames I get the first small errors like 22,5 and it goes up to 30 with 30 frames O.o

But I am sure vel# = 30 / (30 /20) = 20 O.o

Someone know what cause this strange problem?

System: Win 8.1 64 / Core i5 (3,9GHz), GeForce 760 GTX 192 - Bit Dx 11 (Driver 335.23) / 8GB Ram
Ortu
DBPro Master
18
Years of Service
User Offline
Joined: 21st Nov 2007
Location: Austin, TX
Posted: 29th Aug 2014 01:02 Edited at: 29th Aug 2014 01:06
Floats have a measure of imprecision to start with, mixing floats and integers will only make things murkier.

If you want vel# to always be 20 regardless of fps, why even do a calculation?

You can also reduce to one function call to screen fps() by just setting it to start with



Green Gandalf
VIP Member
21
Years of Service
User Offline
Joined: 3rd Jan 2005
Playing: Malevolence:Sword of Ahkranox, Skyrim, Civ6.
Posted: 29th Aug 2014 01:33
More precisely, the problem is that your expression uses integer arithmetic so 30/(30/20) becomes 30/1 (because the real number 1.5 (=30/20) gets rounded down to the integer 1) and then 30/1 evaluates to 30.

If you want more exact division using floats (which as Ortu suggest only have a few significant figures accuracy) then you could do something like



That would force the more accurate float division throughout because the first expression evaluated is fps/20.0 and the presence of a float forces conversion to a float which in turn forces the next division to be a float as well.

But why would you want that anyway? As Ortu says, why not simply write



etc?



Powered by Free Banners
Fane
14
Years of Service
User Offline
Joined: 9th May 2012
Location:
Posted: 29th Aug 2014 09:01
Thanks guys but after 12 hours of programming yesterday I dont get this simple idea anymore

@Green Gandalf: Good to know with vel# = fps / (fps / 20.0)

System: Win 8.1 64 / Core i5 (3,9GHz), GeForce 760 GTX 192 - Bit Dx 11 (Driver 335.23) / 8GB Ram
Libervurto
20
Years of Service
User Offline
Joined: 30th Jun 2006
Location: On Toast
Posted: 31st Aug 2014 11:53 Edited at: 31st Aug 2014 12:07
Were you drunk when you wrote this or are you just new? Either way you will be laughing at this code in a week.

I agree with Ortu's code but I would also store vel# = 20.0 as a constant, in case you want to change its value in future.

Quote: "Thanks guys but after 12 hours of programming yesterday I dont get this simple idea anymore"

It is like calculating 6 + 1 - 1 = 6, it is a waste of time because you are just cancelling out the calculation. This might not be so obvious with division... [goes off to make a latex example]

You should remember this from algebra



Formerly OBese87.
Ortu
DBPro Master
18
Years of Service
User Offline
Joined: 21st Nov 2007
Location: Austin, TX
Posted: 6th Sep 2014 05:50
Quote: " I would also store vel# = 20.0 as a constant, in case you want to change its value in future."


good point

Login to post a reply

Server time is: 2026-07-05 17:32:54
Your offset time is: 2026-07-05 17:32:54