[java] Hmmm...algorigthm troubles...or something

Started by
13 comments, last by fadilthrejk 20 years, 6 months ago
i wasn''t asking for the answer, just where i was going wrong in my algorithm. i still don''t really know where i''m losing a penny.
Advertisement
You''re probably losing the penny due to rounding errors in the floating-point types. The same problem occured in the *judge''s* code for a change-making problem at the ACM pacific northwest regional programming competition last year... Made a right mess of things while they sorted that out.

I suggest that you not use floating-point types anywhere. Find a way to solve the problem entirely in integers. (And yes, there are many ways to do it.)
I ran your program a few times and got accurate results each time. What number are you entering that is not working correctly?


First make it work,
then make it fast.

--Brian Kernighan

The problems of this world cannot possibly be solved by skeptics or cynics whose horizons are limited by the obvious realities. We need men and women who can dream of things that never were. - John Fitzgerald Kennedy(35th US President)

Do not interrupt your enemy when he is making a mistake. - Napolean Bonaparte
"None of us learn in a vacuum; we all stand on the shoulders of giants such as Wirth and Knuth and thousands of others. Lend your shoulders to building the future!" - Michael Abrash[JavaGaming.org][The Java Tutorial][Slick][LWJGL][LWJGL Tutorials for NeHe][LWJGL Wiki][jMonkey Engine]
i figured it out. i was losing it in the decimal place rounding. it''s cuz a double cannot be represented perfectly in binary. i have to do it another way.
if you need more precision you could use the class BigDecimal, which has arbitrary precision.

but personally i''d prefer using only integers. an algorithm with only ints isn''t too hard.

-smoke

This topic is closed to new replies.

Advertisement