floating point value

Started by
34 comments, last by phil67rpg 10 years, 8 months ago
First off, when dealing with money, avoid floats entirely. Floating point inaccuracies can trivially make generating the correct amount of change WRONG very quickly. This is why most languages that deal with banking have a "decimal" datatype, that is base 10.

Secondly, use modf, as I mentioned above. Makes it pretty trivial:
#include <iostream>
#include <string>

int main() {
	float decimal_pounds;
	std::cout << "Enter decimal pounds: ";
	std::cin >> decimal_pounds;

	float pounds, shillings, pennies;
	float remainder;
	remainder = modf(decimal_pounds, &pounds);

	remainder *= 20.0; //convert to shillings
	remainder = modf(remainder, &shillings);
	
	remainder *= 12.0; //convert to pennies
	remainder = modf(remainder, &pennies);
	
	std::cout<<pounds<<" - "<<shillings<<" - "<<pennies<<" - "<<remainder<<std::endl;
	return 0;
}
Take note that if you use a value like 12.23 you get a result out with a remainder whose value is NOT the expected 0.2, but will be a value close to 0.2! This is due to floating point errors.

As an additional note, if you accumulate those lost decimal points into an account... you end up having to burn down your place of work and take up construction.

In time the project grows, the ignorance of its devs it shows, with many a convoluted function, it plunges into deep compunction, the price of failure is high, Washu's mirth is nigh.

Advertisement

well I tried your code and it works fine, but I am still confused on how to calculate pence from my code, I really want to learn how to code.

well I tried your code and it works fine, but I am still confused on how to calculate pence from my code, I really want to learn how to code.


Well, i suggest stepping through your code with a debugger. The bug is quite obvious, and learning to use a debugger will help you to understand more.

In time the project grows, the ignorance of its devs it shows, with many a convoluted function, it plunges into deep compunction, the price of failure is high, Washu's mirth is nigh.

cool thanks

one more little question, how do I put a number like 10.2.8 into a variable.

That's not a number.

In time the project grows, the ignorance of its devs it shows, with many a convoluted function, it plunges into deep compunction, the price of failure is high, Washu's mirth is nigh.

Given that you're using a weird currency and want to get things as integers instead of decimals it'd probably be simplest to just make a class that represents the currency and has methods to get it in different values.

But you might have to read up on classes to do that one.

You would write a class that represents pounds, shillings and pence and would provide constructors to initialise it, and provide public methods (and/or free functions taking an object of your class as an argument) to do things with it that would be useful, like adding, subtracting, getting a string representation, etc.

EDIT: An internal representation as an integer number of pennies is one way of doing it, although not if you want farthings or halfpennies to be represented too.

"Most people think, great God will come from the sky, take away everything, and make everybody feel high" - Bob Marley

I have not got to classes yet in my book, there must a simple way of storing pounds shillings and pence.

There is, just store the number of pence, and output it in the correct format via a function call.

Then you would wrap that in a class. It's similar to how computers could store the date, they could store the number of milliseconds since some agreed start point, but provide functions to output it in a nice format (and functions to convert various formats for the date into the internal representation). EDIT: And provide other functions to do common tasks with dates.

"Most people think, great God will come from the sky, take away everything, and make everybody feel high" - Bob Marley

This topic is closed to new replies.

Advertisement