# Board index » delphi » Floating point error in an expression for evaluating investments

## Floating point error in an expression for evaluating investments

I am using the following formula for calculating the percentage an
investment has given at the current moment:

% = Power ( Current value / Initial value , 360 / days total )

I maintain a file wher each record holds the individual data of each
investment. Therefore, the above formula translates  into the following
Delphi expression:

StckTblPercent.Value:= (StckTblCurrent.Value/StckTblInitial.Value ,
360/StckTblDays.Value )

This seems to work OK but in one case: if the initial value gets negative, I
get an error "invalid floating point operation". The initial value can get
negative, since it represent the cumulative net amount of possibly numerous
purchases and sales of the specific investment item.

Given that, I have two questions :
- why do I get the floating point error
- am I using the wrong way for evaluating an investment ? What I wish is
simply understand whether I am gaining (?!) or loosing, and how much, having
spent \$100 3 years ago, 200 more one year ago and perhaps cashed \$50 or
something like that

Here is an example:
01.01.1999   Purchase  IBM stocks   90 x \$100      \$9,000.00
05.01.1999   Purchase  IBM stocks   10 x \$100      \$1,000.00
01.01.2000   Sale  IBM stocks          50 x \$120      \$ 6,000.00
01.01.2000   Dividends                                           \$    100.00

Current portfolio situation:
IBM stocks     50      current quotation  \$90    current value \$
4,500.00
Total costs \$ 10,000     Total earnings \$ 6,100   Net costs  \$ 3,900

## Re:Floating point error in an expression for evaluating investments

On Mon, 24 Sep 2001 22:06:34 GMT, "Franco Jommi" <franco.jo...@tin.it>
wrote:

##### Quote
>I am using the following formula for calculating the percentage an
>investment has given at the current moment:

>% = Power ( Current value / Initial value , 360 / days total )

>This seems to work OK but in one case: if the initial value gets negative, I
>get an error "invalid floating point operation". The initial value can get
>negative, since it represent the cumulative net amount of possibly numerous
>purchases and sales of the specific investment item.

You can't raise negative numbers to powers, since the formula used for
Power(x,y) is exp(y*ln(x)).  You should never have to do so in
financial calculations.  You need a more sophisticated way to do your
calculations.  If you have a purchase of \$100 which triples to \$300,
then a sale of \$200, the remaining value doesn't represent the value
of an investment of -\$100, it represents the value of an investment of
\$33, since you sold 1/3 of your initial investment.

Duncan Murdoch

## Re:Floating point error in an expression for evaluating investments

Thanks Duncan for your insight. Of course I agree that I need a more
sophisticated way for doing my calculations and this is exactly what I am
after with this discussion.
I am not convinced that I can say which part of my investment is represented
by what I have left in value..
If I think to the real case, in my example the summary is that:
- I spent                        \$ 10,000   (in two different dates)
- I cashed                       \$ 6,100    (in two different dates)
- I still have a value of     \$ 4,500     (now)
The problem is that each figure represents a different value if you take
into account "time".
If the case was that: I spent 1 year ago, I cashed now and now I evaluate
the investment, then the formula would work.
Another approximation that woukd avoid the floating point problem could be
to add all of the earnings to the final value. What do you think ?

"Duncan Murdoch" <dmurd...@pair.com> ha scritto nel messaggio
news:i6hvqt8nh154nmimjibrd1dviv0cas2ram@4ax.com...

##### Quote
> On Mon, 24 Sep 2001 22:06:34 GMT, "Franco Jommi" <franco.jo...@tin.it>
> wrote:

> >I am using the following formula for calculating the percentage an
> >investment has given at the current moment:

> >% = Power ( Current value / Initial value , 360 / days total )

> >This seems to work OK but in one case: if the initial value gets
negative, I
> >get an error "invalid floating point operation". The initial value can
get
> >negative, since it represent the cumulative net amount of possibly
numerous
> >purchases and sales of the specific investment item.

> You can't raise negative numbers to powers, since the formula used for
> Power(x,y) is exp(y*ln(x)).  You should never have to do so in
> financial calculations.  You need a more sophisticated way to do your
> calculations.  If you have a purchase of \$100 which triples to \$300,
> then a sale of \$200, the remaining value doesn't represent the value
> of an investment of -\$100, it represents the value of an investment of
> \$33, since you sold 1/3 of your initial investment.

> Duncan Murdoch