Sign in to follow this  
Handyman54

Helping people new at programming!

Recommended Posts

Hi all! I would like to tell any newbies to programming that i am willing to help with any problems you are having! I am new at programming also but I am learning more every week at my school. I right now at this time am making a text adventure. So if you have any questions just call on me! P.S. does anyone know how to put a random monster generator and how to subtract as in money? It's for my game. I just need a refresher on that! Thanks all

Share this post


Link to post
Share on other sites
Quote:
Original post by Handyman54
Hi all! I would like to tell any newbies to programming that i am willing to help with any problems you are having!

Great!
Quote:
P.S. does anyone know how...subtract?

While I admire your willingness to help, Handyman, keep in mind that sometimes it is more helpful to remain silent then to offer up guesses that aren't supported by experience. Learning to tell good advice from bad is a very difficult skill for a lot of people, and many of those people are in the For Beginners forum. The best thing you can do for these people is concentrate on learning yourself, so that you can better help them in the future.

Share this post


Link to post
Share on other sites
I would imagine that if you were using the modern day american system, you would either multiply the amount by 100 and get how much cents, or use one int for the dollar amount and one for the cents, trying to avoid floats as much as possible.

Share this post


Link to post
Share on other sites
Quote:
Original post by ender7771trying to avoid floats as much as possible.


....Why?

Id either just use a float, or probably better still just store the amount in cents, which you can then display as dollars/cents fairly easy. You could even write a little class that handles it all (and overload the + - operators...)

Handyman, being available to help others is always a good thing. Just be sure your not ever giving out bad or incorrect advice ;) Spend some time making sure you fully understand everything your doing first =)

Share this post


Link to post
Share on other sites
Quote:
Original post by Anonymous Poster
floats and doubles have a limited precision resulting in inaccuracies


Well, its only ever gonna need to go to 2 decimal places. As long as your not allowing inputs like $53.35436789 there shouldnt be a problem...

Share this post


Link to post
Share on other sites
Not true. 53354367.89 would be just as big a problem as 53.35436789 is.
It doesn't really matter how many decimal places you have, it's how many digits total (and even that's not the full story. You can get inaccuracies with much shorter numbers too. Like, say, 2.1)

If you use doubles/floats, you *will* get rounding errors. The question is whether that's a problem in your case.

Share this post


Link to post
Share on other sites
I brought up the fact that you should stay away from floats because I recall a while back (maybe like a month or so) someone had a program that figured out change. The algorithm was great and everything, but because there was floats the rounding errors made the program bad. Anyone else remember?

Share this post


Link to post
Share on other sites
Quote:
Original post by ender7771
I brought up the fact that you should stay away from floats because I recall a while back (maybe like a month or so) someone had a program that figured out change. The algorithm was great and everything, but because there was floats the rounding errors made the program bad. Anyone else remember?

Yeh, I remember. Basically, the problem was that successive subtractions were producing a number that was something like 1.999999997 instead of 2, and this was screwing up the change-making algorithm.

Floating point numbers should never be used as counts ("three apples"), only as amounts ("two cups of sugar").

Share this post


Link to post
Share on other sites
But integers have a far-larger rounding error than floats!(or doubles) I don't understand why using an integer will make your rounding problems go away. (Not that anyone has stated having a rounding error yet)

As for your questions, were going to need a little more information on that. Theres the rand() function if youre using c++. That will generate a random number. Just run that and an Integer through a modulus operator and you can generate a fairly-random number between 0 and the Interger - 1. As for subtracting, you could use the subraction operator if its for intergers/floats...

Share this post


Link to post
Share on other sites
Quote:
Original post by Ezbez
But integers have a far-larger rounding error than floats!(or doubles)

No, actually using integers maths can lead to spot on exact solutions.

Quote:
I don't understand why using an integer will make your rounding problems go away. (Not that anyone has stated having a rounding error yet)

Consider a rational number class. Using integers, you'd store the numerator and the denomitator. For floats, just store the float. Over time, the float will suffer from inaccuracy caused by rounding errors while the integer'ed version will contain the exact answer no matter how many equations you put it through.

Share this post


Link to post
Share on other sites
I understand how float point inaccuracies surface, but for this example i didnt see any real reason to stay away from using float values.

*assuming* that inputs are rejected unless they are at the most to 2dp, if your only ever adding and subtracting then rounding errors wont appear.

64.36 + 50.02 is always going to be exactly right, as will 45.74 - 34.52. Thats assuming i havent made a huge huge mistake in how float values are represented internally :]

If for some reason these values were being pushed through many different equations then maybe this solution wouldnt be for the best. But since the original question was asking about how to subtract a value, im assuming that isnt the case :)

Share this post


Link to post
Share on other sites
Quote:
Original post by Ezbez
But integers have a far-larger rounding error than floats!(or doubles) I don't understand why using an integer will make your rounding problems go away. (Not that anyone has stated having a rounding error yet)

No, integers have no "rounding error". Their rounding is exact and well-defined. Floating point numbers, on the other hand, do have rounding error, and that rounding error is (to some extent) implementation-defined. This causes problems with operations that expect exact values, such as equality and modulus.

Share this post


Link to post
Share on other sites
To convince yourself that floats aren't good enough for change-making, try subtracting 999.01 from 1000. I get 0.98999, so already you have to modify your output to account for inaccuracy (even if it rounds off correctly).

Now if this is some game with severe inflation, and you have a million "dollars," subtacting 1000000.00f - 999999.01f results in 1. At that point, you've lost a cent.

(You might have to add another zero or two depending on the compiler or settings, but it will break down after a certain amount)

Share this post


Link to post
Share on other sites
Quote:
Original post by dudedbz1
I disagree. Floating point numbers are exact.

No, they're not.

Quote:
On my system when I do ...

That's great on your system. But any compiler is free to implement floats however they please. Floats are an approximation; whatever you set them to, the result will be *approximately* the same. Depending on the value, the approximation may be enough to lead to erroneous program behaviour (especially if you rely on the same float without normalizing it).

Share this post


Link to post
Share on other sites
Quote:
Original post by dudedbz1
//Edit2: Huh? 1000.0(or just 1000)- 999.01 gives the right answer. When you have 1000.0f - 999.01f it gives the wrong answer. Thanks for nothing to all of you that said that you need an 'f' in the end!


Without the "f", the numbers are interpreted as type double which are twice as precise as floats (64 bit) on several compilers.

Share this post


Link to post
Share on other sites
Quote:
Original post by dudedbz1
//Edit: 1000 - 999.01 gives the correct answer. I dont know why when I have 1000.0f it gives the wrong answer(what you got).
//Edit2: Huh? 1000.0(or just 1000)- 999.01 gives the right answer. When you have 1000.0f - 999.01f it gives the wrong answer. Thanks for nothing to all of you that said that you need an 'f' in the end!

So, basically... you're guessing?

C# includes a decimal type as distinct from float and double to solve exactly this problem. In most business environments, a binary-coded decimal representation is used as it is more compact and memory efficient than using integers, and consequently can represent much larger numbers (your binary-coded decimal can easily be extended to a "big integer"-type value, limited in representation solely by the amount of RAM you have, not by the system word size).

For a problem being discussed in the For Beginners forum, using cent-based integer representation for monetary amounts (or the equivalent for your unitary system) should suffice. For larger problems, get to working on your 4-bit BCD.

Share this post


Link to post
Share on other sites
If you intend to use floats or doubles to represent exact amounts, you're asking for trouble. Floats and doubles are the same as ints in that they are all based on powers of 2. There are many values within the range of a float that cannot be exactly reproduced because of this. So, for example, in places where you expected 2.1, you may end up getting 2.09999..., because 2.1 could not be repoduced properly.

If you're going to be dealing with money, use ints and treat all values internally as cents. Convert to/from dollars for input/output only.

Share this post


Link to post
Share on other sites
Since I don't see that it's been addressed, Handy, I'll take the liberty to say that, for a random-monster generator, I'd just make an object factory that spit out monsters...monsters all being derivations inherited from some core monster structure.

If you haven't learned about "objects" or "factories" yet =) I recommend looking into it. It's all fundamental in a lot of software engineering these days. What level of programming are you at in school right now?

Oh, and as for the money thing, I agree with what I saw written above...just make it an int and when you display it, casually insert a decimal point in like...two digits from the right =)

Good luck.

Share this post


Link to post
Share on other sites
i think the point should be raised that most text adventures only use "gold coins" or similar, rather than dollars and cents, avoiding the problem raised entirely.

So handyman what is the currency of your adventure?

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this