|
Thread Rules 1. This is not a "do my homework for me" thread. If you have specific questions, ask, but don't post an assignment or homework problem and expect an exact solution. 2. No recruiting for your cockamamie projects (you won't replace facebook with 3 dudes you found on the internet and $20) 3. If you can't articulate why a language is bad, don't start slinging shit about it. Just remember that nothing is worse than making CSS IE6 compatible. 4. Use [code] tags to format code blocks. |
On October 29 2015 00:25 _Grazze_ wrote: Yo dudes! I'm trying to program a change calcuator in Java for an assignment I have in school. It works kinda okay but it wont print out cents. Any ideas?
Code in spoiler.
Yo dude! This is not the place where people correct your homework!
That being said, think about the line where you convert your money from double to int.
|
Also, why the hell are you even using doubles? Just do everything in cents (int) and convert it to different value only for printing purposes... Much easier and safer.
|
|
On October 29 2015 01:46 Nesserev wrote:Show nested quote +On October 29 2015 01:34 Manit0u wrote: Also, why the hell are you even using doubles? Just do everything in cents (int) and convert it to different value only for printing purposes... Much easier and safer. I think the insight of doing everything in cents is something that comes with a certain notion of experience, even if its tiny. Would 'newb' Manit0u X years ago have tackled this problem by doing everything in cents from the get-go, even if someone hadn't shown it to him before? Would that really be your first idea? I don't know you... but I know that newb me would have done the same thing as him, and used doubles first.
That's why I suggested using cents instead of doubles. I also didn't know about that until I've discovered that Symfony framework introduced special form field type for this purpose. Examining how it worked was enlightening. Now I'm sharing the wisdom
|
Luckily I learned that doubles are an awful way to represent an amount of money long before I ever needed it. Too bad that we still use doubles for that at work anyways.
Ideally, you would have a Money class that bundles amount and currency. It can also provide common meney-related functionality and information like the appropriate formatting. For a small project that would be overkill, of course.
|
On October 29 2015 02:32 spinesheath wrote: Luckily I learned that doubles are an awful way to represent an amount of money long before I ever needed it. Too bad that we still use doubles for that at work anyways.
I would never voluntarily use doubles for money, but if you are forced to do it:
you can get away with it, as long as you are careful when you do arithmetic. Here is an example.
|
A lot of textbook examples will just go ahead and use doubles for currency. The efficiency of the program itself isn't really their main concern I guess.
|
On October 29 2015 02:59 phantomfive wrote:Show nested quote +On October 29 2015 02:32 spinesheath wrote: Luckily I learned that doubles are an awful way to represent an amount of money long before I ever needed it. Too bad that we still use doubles for that at work anyways.
I would never voluntarily use doubles for money, but if you are forced to do it: you can get away with it, as long as you are careful when you do arithmetic. Here is an example. It's no big deal in my case because we just display prices as a point of reference and then pass the order on to a different system. It's still kinda sad.
|
On October 29 2015 03:43 Itsmedudeman wrote: A lot of textbook examples will just go ahead and use doubles for currency. The efficiency of the program itself isn't really their main concern I guess. It's about mistakes, not efficiency. For example this here:
0.1 + 0.2
That's not 0.3 with floats and doubles. Weird stuff like this happens:
> 0.1 + 0.2 == 0.3 False
> 0.1 + 0.2 < 0.3 False
> 0.1 + 0.2 > 0.3 True
You need to fix it like in phantomfive's example, use rounding to force results into two digits after the decimal point to make things work correctly.
|
Even if you round, the binary representation of fractions is still a problem. To avoid other problems, use a delta in comparisons.
double delta = 0.00001; // max deviation/fault tolerance Math.Abs(0.3 - (0.1 + 0.2)) < delta
|
So awhile ago I found this math library (C++) that had a bunch of functions useful for screen animations and transitions and such. For example, you can have menu button start by moving fast, but as it approaches where you would like it too land, it will slow way down. Or, move slowly at first, make fast progress in the middle, and then slow down again (like a bell curve). It was a pure math library, im just describing how i used it with graphics.
Anyway, the question is, do you know what keywords I should I should try to hopefully find it again? I tried stuff like C++ timestep smoothing smoothstep, and various combinations but i don't really know what a collection of those formulas would be called.
|
Hey y'all, I've got a design question. I'm trying to implement logging for my project in Python. We log normally, to a daily log file. Then, at the end of the day we switch to the log file for the next day. I'm planning on giving it a 10 second delay(to avoid problems with logged items right at midnight), then uploading the log file to our cloud storage, so we only have the log file for one day at a time.
However, I'm wondering what the best way to do this would be. My current idea is to have a "heartbeat" script on our server that starts when the server starts. It initiates a sched.enterabs, which will run a function at a specific time of day, 10 seconds after midnight. However, this has a few problems: if we start the server between midnight and 10 seconds after midnight, then we should have a problem with the day being off by one. Further, it's not threadsafe. If we are simply waiting for the event we've tied up a whole processor with that.
I'm thinking that I should use threading.timer. Then I can simply run the script on startup. It would then find the amount of time until the next time I want to upload the log, and wait that long. However, it then has a problem with running it per day. I'm not sure exactly how to deal with that problem.
Anyway, if anyone has suggestions on how to deal with this I'd appreciate them. Part of this has been rubber ducky programming for me to try to figure it out. Peace!
|
On October 30 2015 15:12 WarSame wrote: Hey y'all, I've got a design question. I'm trying to implement logging for my project in Python. We log normally, to a daily log file. Then, at the end of the day we switch to the log file for the next day. I'm planning on giving it a 10 second delay(to avoid problems with logged items right at midnight), then uploading the log file to our cloud storage, so we only have the log file for one day at a time.
However, I'm wondering what the best way to do this would be. My current idea is to have a "heartbeat" script on our server that starts when the server starts. It initiates a sched.enterabs, which will run a function at a specific time of day, 10 seconds after midnight. However, this has a few problems: if we start the server between midnight and 10 seconds after midnight, then we should have a problem with the day being off by one. Further, it's not threadsafe. If we are simply waiting for the event we've tied up a whole processor with that.
I'm thinking that I should use threading.timer. Then I can simply run the script on startup. It would then find the amount of time until the next time I want to upload the log, and wait that long. However, it then has a problem with running it per day. I'm not sure exactly how to deal with that problem.
Anyway, if anyone has suggestions on how to deal with this I'd appreciate them. Part of this has been rubber ducky programming for me to try to figure it out. Peace!
You might want to look into logrotate. It essentially does all of that already.
In your project you simply write to a file and at the time you specify, logrotate renames the old file and creates a replacement log file seamlessly and any script you start afterwards writes to the new file. It can also gzip older logs and does a bunch of other fancy things.
|
On October 29 2015 04:39 supereddie wrote:Even if you round, the binary representation of fractions is still a problem. To avoid other problems, use a delta in comparisons. double delta = 0.00001; // max deviation/fault tolerance Math.Abs(0.3 - (0.1 + 0.2)) < delta
All cool and dandy, but what do you want to compare here?
You have 2 doubles and you want to see if the result of an operation performed on them is within delta. How do you determine what do you want to compare the result against?
It's nice when talking about 0.1, 0.2 and 0.3 for the sake of examples, but what if you don't know any of them beforehand?
|
Thanks! That led me on to looking at cron jobs, which I think is what I want. If I use cron to schedule it daily then I can run it in Python, which is necessary to deal with the files nicely I believe - they have to get uploaded to s3. Appreciate the help!
|
On October 31 2015 04:25 WarSame wrote: Thanks! That led me on to looking at cron jobs, which I think is what I want. If I use cron to schedule it daily then I can run it in Python, which is necessary to deal with the files nicely I believe - they have to get uploaded to s3. Appreciate the help!
If you're using python logging module there's rotating handler and or syslog handler depending on how you want to handle.
Even if you go the logrotate & cron job option, I'd advise using the logging module out to a normal file.
|
My current setup is to logout to a normal file for the day(so today would be "logfile-instanceName-2015-10-30.txt") with the python logging function(passed through a handler that I made). At the end of the day I plan to use cron which will run a script called rotateLogs.py. This accesses a function inside my main logging file, localLogging.py. The function is called rotateLogs() and it uploads the previous day's log, and then deletes it. Logging automatically switches to the new day's file because I determine the day of the file every time I try to log something(which is probably inefficient but our log traffic is very low). TimedRotatingFileHandler kinda looks like what I'm looking for, but I don't think can call a function on itself when its time is up.
|
On October 29 2015 16:38 Amnesty wrote: So awhile ago I found this math library (C++) that had a bunch of functions useful for screen animations and transitions and such. For example, you can have menu button start by moving fast, but as it approaches where you would like it too land, it will slow way down. Or, move slowly at first, make fast progress in the middle, and then slow down again (like a bell curve). It was a pure math library, im just describing how i used it with graphics.
Anyway, the question is, do you know what keywords I should I should try to hopefully find it again? I tried stuff like C++ timestep smoothing smoothstep, and various combinations but i don't really know what a collection of those formulas would be called.
When I wrote a library with graphics like that, I used some Bezier curves. They were easy to implement with a speed and an acceleration.
The explanations of Bezier curves on the internet are a little dense, but once I got through that, the concepts are really simple.
|
I remembered the name. The group of functions I was looking for are commonly referred to as easing functions.
|
On October 31 2015 03:54 Manit0u wrote:Show nested quote +On October 29 2015 04:39 supereddie wrote:Even if you round, the binary representation of fractions is still a problem. To avoid other problems, use a delta in comparisons. double delta = 0.00001; // max deviation/fault tolerance Math.Abs(0.3 - (0.1 + 0.2)) < delta
All cool and dandy, but what do you want to compare here? You have 2 doubles and you want to see if the result of an operation performed on them is within delta. How do you determine what do you want to compare the result against? It's nice when talking about 0.1, 0.2 and 0.3 for the sake of examples, but what if you don't know any of them beforehand? The delta should be an application or business rule. The others can just be variables or something. Basically anywhere you do an equals comparison between floats/doubles you can replace it with this. Useless example:
// Somewhere else internal static readonly double FaultTolerance = 0.001;
private bool IsOneThird(double someNumber) { // any someNumer between 0.332 and 0.334 is counted as 1/3 return (Math.Abs(0.33 - someNumer) < Constants.FaultTolerance); }
|
|
|
|