I'm hoping that someone out there might be able to help me solve a problem I've been battling for the past couple of days. I'm trying to update my app for android and ios (link to google play) but I've hit the limit of my understanding of how computers do maths... The app is not a game, it's a time calculator (ie it does base 60 maths for hours and minutes addition and subtraction) and I'm trying to update it to offer decimal hours as an input option. So now I want it to be able to add floating point variables and convert said fractions into hours and minutes (using simple code if possible!).
Presently the data is stored in arrays for hours and minutes and I'm sure I could fudge the current event sheet to make it work - but the OCD parts of my brain want to make the new version look neat and tidy.
The problem is that when using floating point variables, the 'int', 'abs', "%" and other mathematical system expressions aren't reliable for my needs. C2 seems to round variables depending on their context and I'm stuck. Is there a simple and reliable way to convert a number such that the whole number can be displayed in base 10 and the decimal fraction can be converted to base 60... a way that will also work with negative numbers?
Let me elaborate. Subtracting int(N) from N doesn't always give the decimal fraction that remains - see this capx to demonstrate what I mean.
Does this mean that I'm going to have to convert my data into text strings and then manipulate the string to get the answers I want, or can someone think of another way...? If I need to I will, but I'm hoping for an elegant solution...
I've tried following this thread's advice but it doesn't work for me, especially regarding negative numbers:
Thanks for reading this far!
I know, C2 is meant to build games... I couldn't help myself!
Why work in base 60 ? Why not just convert everything into minutes do the maths and then convert back ? No need for decimal places.
Have you looked into using unix time stamps? I wrote a calendar app and used the rex_date plugin. You can then convert any date (and time) into a timestamp, and then add it or subtract from any other timestamp - down to millisecond accuracy.
Well I am still having problems why anyone would want to use decimal time - but then again I am a bit of a luddite when it comes to new ideas..
Anyway here are two caps for you to peruse :)
Using the plugin is probably a much easier way to do this...
RamPackWobble and AllanR,
Thank you both for your advice. I particularly appreciate the capx examples. I'll have a look at them today and let you know if they work for me - although I think your first point was excellent because it gave me some clarity on the issue... I seem to have become wrapped up in trying to solve a technical challenge rather than finding a workable solution to my update problem. I don't have time to look at the examples at the moment (day job in the way of fun) but I think I'm going to try to convert my time to minutes and store that data - that way there'll not be any floating point nonsense!
Develop games in your browser. Powerful, performant & highly capable.
I'm going to call this closed because I've had the help I needed to get things moving forward.
RamPackWobble - thanks for the examples. It's going to take my slow brain a while to understand the maths behind test 1 but it works a treat.
AllanR - I'm downloading the plugin now - thanks for the tip, it might just do the job.
Colludium, Using the plugin to convert to a timestamp is pretty much the same idea as what RamPackWobble was suggesting because it returns a single integer, only it also incorporates the date (as well as seconds and milliseconds). Then you can subtract one timestamp from another one and calculate how many days, hours and minutes is between the two.
Or, if you want to add 4.75 hours to a timestamp you would have to convert 4.75 hours into milliseconds, Round(4.75 * 60 * 60 * 1000) = 17100000, then add that to your starting timestamp.
One thing to watch out for with the plugin is that date.day(timestamp) returns the day of week, not the day of month, use date.date(timestamp) to get the day of the month. This confused me at first, and obviously others if you read through the forum thread for the plugin.
so, if you set a global variable "CurrentTime" = date.UnixTimestamp
then ask it for date.day(CurrentTime) you would get 5 for Friday, while date.date(CurrentTime) would return 31 because today is the 31st of January.
The rest work the way you would expect (Year, Month, Hours, Minutes, Seconds).
To get a timestamp for a given date/time (for example 1:30 pm tomorrow) use: Date.Date2UnixTimestamp(2014,2,1,13,30,0,0)
Thanks AllanR for the help. Although I'm a novice at time stamps, it would appear that the plugin could enable me to provide even more advanced features on my app in future (so that duty hours could be tracked over a rolling 7 day period for example - something for the next but one update I think!). Just for interest, my solution to this data crunching: I've decided to convert the input values using a common denominator so that minutes and decimal (1/100th) hours can be converted to/from each other without the answers resulting in those pesky floats. It's quite simple now I think about it... <img src="smileys/smiley1.gif" border="0" align="middle" />