Click to See Complete Forum and Search --> : Best Language for data accuracy


tonymontana
December 8th, 2008, 05:33 AM
Hi,

I am trying to write a simple trading program that reads data from a text file, crunches a few numbers, then prints out the results to a few files. Nothing complex. I have tried to write the program in C++ but am having difficulty in the loss of precision using floats.

Which language would be the best to use in this scenario? Again, the most important aspect of the program is the accuracy of the data.

Thanks

boudino
December 8th, 2008, 06:40 AM
It is not primary matter of language, but of data type. Floats are naturaly unprecise. You need some fixed point numbers like Decimal in C#.

TheCPUWizard
December 8th, 2008, 08:21 AM
It is not primary matter of language, but of data type. Floats are naturaly unprecise. You need some fixed point numbers like Decimal in C#.


Agreed. There are also plenty of fixed precision (including arbitrary precision) math libraries for C++.

The language decision should NOT be influenced by the mathematics (in this case). What ever language is chosen, the math must simply be cone appropriately.

toraj58
December 8th, 2008, 08:34 AM
in c++ you can use double or long double for more precision.

also C++ decimal support is provided in the bcd class. The header file is bcd.h

C++ is powerful language and there is no need you change the programming language for solving such simple problem.

but a comment: if you are new to C++ i would say you that writing such program in C# is easier than C++ then perhaps it worth you to manage to learn c# and write that program with C#.

tonymontana
December 12th, 2008, 03:05 AM
Thanks for your input guys.

laserlight
December 12th, 2008, 03:12 AM
in c++ you can use double or long double for more precision.
Nonetheless boudino's point that "floats are naturaly unprecise" still stands.

also C++ decimal support is provided in the bcd class. The header file is bcd.h
That is misleading since "the" bcd class is non-standard.

toraj58
December 12th, 2008, 12:14 PM
i am not talking about absolute percission even decimal is not absolutly precise. but in a program base on the provlem level od percesion should be considered. for a some programs like games float is good but for an accounting program Decimal.
about bcd.h it has been used in several math projects.

what is your point.....give him your solution.

TheCPUWizard
December 12th, 2008, 12:22 PM
i am not talking about absolute percission even decimal is not absolutly precise.

Decimal representations are 100% precise. Regardless of the number of decimal positions (before or after the point) ever mathematical operation will be identical to performing the same calculation on paper.

Many (most?) operations can be accomplished in C++ simply by using "scaled" integers. For example keeping (US) currency amounts in "pennies"

int x = 500; // this is $5

or even thousandths of a cents.

long = 500000; // this is $5


For clarity, it may mke "sense" [pun intentional] to write a trivial wrapper class so that these items can be distinguised from other numerical values.

toraj58
December 12th, 2008, 12:47 PM
CPU look,

i am talking about absolute percision for math:

decimal range is like this:

10^-28 and 10^28

i give you a simple e.g.

lim (1/n) when n -> infinity = 0

would you please write a program with c++ using Decimal that give me 0 for above formula.
don't use any other algorithm or estimaation that applies to LIM.

in math absolution is x = y where if x has 1,000,000 decimal position y is so and all places are equal. not in the limited range of Decimal i mentioned. i agree that it is big enough for most case but for more comlicated math it is not.

TheCPUWizard
December 12th, 2008, 01:28 PM
You are mixing "precision","range" and "accuracy"...They are different things.

Consider simple integers. In mathematics the prsision is 1, the range is infinite; on a computer the precision is also 1 but the range is limited based on the number of bits and if the value is being treated as signed.

Addition, Subtraction and Multiplication are all 100% accurate [but may cause range errors on a computer]. Integral division is also consistant in that you have an intregal divident and a remainder (divide and modus operations on the computer, but is also 100% accurate.

"Real" numbers become radically different when implemented on a computer using floating point. Here the mathematical precision (resolution) is infinitely small for pure math, but is constrained ot a range on the computer (based on the number of bits in the mantissa). Range is also constrained on the compurer basede on the number obf bits in the exponent.

This limitation of precision makes it impossible to accurately represent values that are within the valid range [note that this can NOT happen with integral (ordinal, natural) numbers].

This limitation occurs regardless of the computers storing mechanism (IEEE floating point, BCD, decimal, et. al.). All of them take the form:

Number = M * (B^E)
where:
M,B,E are integral values with a given range

What causes the complexity for many applications (e.g. Accounting) is that the numbers are accruately defined for B=10, but are NOT capable of being represented for B=2 This is because:

Log2(A) = Log10(B)

does not have a general integral solution for A and B.

Therefore, while the mathematical characteristics are identical for the representaions (in terms of form), the "phase correlation" with the actual values being represented is often much better when B=10 than when other values are used.

toraj58
December 12th, 2008, 01:37 PM
what is difference between precision and accuracy?

TheCPUWizard
December 12th, 2008, 01:53 PM
what is difference between precision and accuracy?


Precision is effectively Resolution.

Accuracy is:

Accuracy = 1.0 - Fractional Error

where the Error can be caused my many different things including (but not limited to) Precision.

Using US currency (dollar) as an example. If your resolution is 0.1, then you will have 100% accruracy providing you are only using bills (integral dollars) and/or dimes ($0.10). However as soon as you introduce quarters ($0.25) or nickels ($0.05) then you will have a potential absolute error of up to $0.05. The Fractional Error will depend on the figure ($23.45 becomes $23.40 or $23.50 which yields (approximately) 99.78% accuracy.)

toraj58
December 12th, 2008, 02:37 PM
okay, thanks.....good example.