Hi everyone , am here again and i need some help, av been struggling with my calculator application and sadly am giving up on it because i don't seem to figure out how to handle precision in my values.
here is my simple ideology, as a was working with my scientific calculator i could simply add,divide,multiply a double to an int and the results will accurately account for the precision that was on the floating point number, This is what i don't seem to get along with easily, do i need to create multiple doubles and int variables and keep them around in case the user decides to use mixed value- this is definitely a bad idea and i don't want to think of, do i default everything "all my variables " to doubles - seems an option but not an accurate one.
kind request: can somebody briefly explain to me how a scientific calculator works to handle mixed values {"both whole numbers and floating points"} without losing precision.
If the variables are all doubles, to me that seems precise enough for a calculator.
You might be interested in typecasting some of your ints to doubles. Typecasting would convert one type to another temporarily. http://www.cplusplus.com/doc/tutorial/typecasting/
Thanks alot @kevin , if you join me on using doubles that's encouraging , i might end up following that route coz it seems to be the only convincing option left.
For the typecast it's really a nice idea but i dont seem to understand how i would apply typecast at runtime;- for example , on the first run the user keys in only doubles meaning i'll not require to typecast anything but on the second run the user uses mixed whole numbers [ints] and doubles , how will i decide among the values entered so that i can extract the int values , cast them and continue with computation? if only this was possible i would definitely go for casting.
I never thought a simple calculator would be this complex, what i simply wanted my calculator to achieve was solve expressions similar to the examples below.
a+d*c
a*d/(c+d%x)
(((a+d)/(a%c))*(b+e-f))
(a+c(e%f)-(d+n)/(q*n))
///later i would add more functions :.. sin,cos, sqrt...
the user would key in such expressions inform of a string , the programs extracts all operators, values and then uses "PEMDAS" to solve the expression.
As you've discovered, getting the math right is surprisingly difficult. It's especially hard to get the answer to look like what a human would expect. This is hard when using floating point numbers that are based on base 2.
Many calculators use binary coded decimal numbers to avoid these problems. Hewlett Packard makes excellent calculators and invented the hand-held scientific calculator. Their recent calcs use 12 or 15 decimal digits for the mantissa and 3 digits for the exponent (with values of -499 to +499) as I recall.
One idea is to keep the values in a separate class with vectors for each type. Then use an overloaded set function.
When two values are calculated, they automatically get converted to the same type, which type depends on a hierarchy of the types. So if you put an int and a double together, the int will be converted to a double for calculation. If the result is being assigned to an int, the result gets converted back to an int, potentially losing data (whatever is to the right of the decimal)
If, instead of being assigned directly to a variable of a set type, the result is instead being passed by value through an overloaded function, theoretically, the value should keep the type from calculation and go to the function of that type.
I intend to test this when get home.
You could then have floats and doubles check if converting to int would lose data, if not, the value could be set as int instead, but if so, it could maintain the current type.
Sounds interesting @dhayden can I be able to use this too binary coded decimal numbersĀ ?An example would be great, 12 or 15 decimal digits am sure I can't get these on my compiler.
@Darklighthitomi how will I get that work sounds awesome Btw i can figure how to do that for example if I would get an expression like these
2*4.5*898/1.3333
, using integers is yet a problem am losing alot of precision.
Having read about this earlier, you are losing precision after the calculation. When the program compares two numbers to perform a calculation it converts the "lesser" type to the "higher" type. The problem then comes when it does something with the result that requires conversion to a lesser type. Not sure how unsigned types play into it though.
I.E. using your example, the int 2 and the float 4.5 are brought forward to be calculated, the int is converted into a float, then the two floats are multiplied. The result is then calculated with the other numbers. If this expression was being assigned to an int however, the final result would be reduced to an int, truncating anything past the decimal.
Thanks guys, i din't give up on the problem, i realized that i needed to do some reading before i could get my problem solved BTW before i went for that option i solved the problem basing it on dirty string parsing methods and performing arithmetic operations using long doubles as my default type, right now it works fine but also it doesn't preserve precision for very small values f.e the result of this expre will be a zero 0.0000000001(0.0000000123) .
probably you wouldn't mind taking sometime have a look at my current progress and pointing out some bad practices av used, for the next couple of days i know i'll be able to come up with some more efficient methods, Don't mind copy pasting and testing it for me , just five minutes are enough, some feedback too would be nice.
http://pastebin.com/qL5yAB1q //i couldn't paste it directly , it's quite large.