comp.lang.ada
 help / color / mirror / Atom feed
* Financial Calculations in Ada
@ 2008-07-20 23:27 rd
  2008-07-21  2:32 ` Keith Thompson
                   ` (2 more replies)
  0 siblings, 3 replies; 6+ messages in thread
From: rd @ 2008-07-20 23:27 UTC (permalink / raw)




I'm playing around some with Ada, and today started with the
intent of cooking up a crappy mortgage calculator.  I did the
obvious:

     type Money is delta 0.01 digits 12;
     package MIO is new Ada.Text_IO.Decimal_IO (Money); use MIO;

I hit a brick wall more quickly than usual in that I couldn't
figure out a good way to handle interest rates that might have
a decimal value with more digits than my Money type, such as 7.125.

Can anyone give me any tips on how to handle this sort of thing
in Ada?

-- 
rd



^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: Financial Calculations in Ada
  2008-07-20 23:27 Financial Calculations in Ada rd
@ 2008-07-21  2:32 ` Keith Thompson
  2008-07-21  5:11   ` tmoran
  2008-07-21  4:46 ` Gautier
  2008-07-21  8:24 ` Graham Stark
  2 siblings, 1 reply; 6+ messages in thread
From: Keith Thompson @ 2008-07-21  2:32 UTC (permalink / raw)


rd <162144@gmail.com> writes:
> I'm playing around some with Ada, and today started with the
> intent of cooking up a crappy mortgage calculator.  I did the
> obvious:
>
>     type Money is delta 0.01 digits 12;
>     package MIO is new Ada.Text_IO.Decimal_IO (Money); use MIO;
>
> I hit a brick wall more quickly than usual in that I couldn't
> figure out a good way to handle interest rates that might have
> a decimal value with more digits than my Money type, such as 7.125.
>
> Can anyone give me any tips on how to handle this sort of thing
> in Ada?

An interest rate isn't an amount of money, so don't use type Money to
represent it.

-- 
Keith Thompson (The_Other_Keith) kst-u@mib.org  <http://www.ghoti.net/~kst>
Nokia
"We must do something.  This is something.  Therefore, we must do this."
    -- Antony Jay and Jonathan Lynn, "Yes Minister"



^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: Financial Calculations in Ada
  2008-07-20 23:27 Financial Calculations in Ada rd
  2008-07-21  2:32 ` Keith Thompson
@ 2008-07-21  4:46 ` Gautier
  2008-07-21  8:24 ` Graham Stark
  2 siblings, 0 replies; 6+ messages in thread
From: Gautier @ 2008-07-21  4:46 UTC (permalink / raw)


rd:

> I'm playing around some with Ada, and today started with the
> intent of cooking up a crappy mortgage calculator.

Is it a crappy calculator, or a calculator for crappy mortgages ?
In the second case, I guess there is right now a strong demand for your 
software :-).
G.
__
PS: seriously, people are selling software to banks to help them 
understanding their risks on asset backed securities, collateralized 
debt obligations, etc.



^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: Financial Calculations in Ada
  2008-07-21  2:32 ` Keith Thompson
@ 2008-07-21  5:11   ` tmoran
  0 siblings, 0 replies; 6+ messages in thread
From: tmoran @ 2008-07-21  5:11 UTC (permalink / raw)


>An interest rate isn't an amount of money, so don't use type Money to
>represent it.
  "delta 0.01" is good for display.  It also works if you only add,
subtract, and multiply by integers.  Once your arithmetic goes beyond
that, precision and rounding become a policy decision. For instance,
at a rate of 7.125% is the total interest, rounded to pennies,
on three $50 loans .07125*50 = 3.5625, 3.56 * 3 = $10.68
Or is it 3*3.5625  = 10.6875, rounded to $10.69?



^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: Financial Calculations in Ada
  2008-07-20 23:27 Financial Calculations in Ada rd
  2008-07-21  2:32 ` Keith Thompson
  2008-07-21  4:46 ` Gautier
@ 2008-07-21  8:24 ` Graham Stark
  2008-07-21  8:30   ` Georg Bauhaus
  2 siblings, 1 reply; 6+ messages in thread
From: Graham Stark @ 2008-07-21  8:24 UTC (permalink / raw)


On Jul 21, 12:27 am, rd <162...@gmail.com> wrote:
> I'm playing around some with Ada, and today started with the
> intent of cooking up a crappy mortgage calculator.  I did the
> obvious:
>
>      type Money is delta 0.01 digits 12;
>      package MIO is new Ada.Text_IO.Decimal_IO (Money); use MIO;
>
> I hit a brick wall more quickly than usual in that I couldn't
> figure out a good way to handle interest rates that might have
> a decimal value with more digits than my Money type, such as 7.125.
>
> Can anyone give me any tips on how to handle this sort of thing
> in Ada?
>
> --
> rd

I've been doing something similar. I would use float types if you can,
not least because the root-finding algorithms you'll have to use
probably won't work with discrete types. As someone else mentioned,
financial institutions will have policies on exactly where and how
money amounts are rounded, but it's unlikely you'll capture that just
by relying on a Decimal type.



^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: Financial Calculations in Ada
  2008-07-21  8:24 ` Graham Stark
@ 2008-07-21  8:30   ` Georg Bauhaus
  0 siblings, 0 replies; 6+ messages in thread
From: Georg Bauhaus @ 2008-07-21  8:30 UTC (permalink / raw)


Graham Stark schrieb:
> On Jul 21, 12:27 am, rd <162...@gmail.com> wrote:
>> I'm playing around some with Ada, and today started with the
>> intent of cooking up a crappy mortgage calculator.  I did the
>> obvious:
>>
>>      type Money is delta 0.01 digits 12;
>>      package MIO is new Ada.Text_IO.Decimal_IO (Money); use MIO;


> I've been doing something similar. I would use float types if you can,
> not least because the root-finding algorithms you'll have to use
> probably won't work with discrete types. As someone else mentioned,
> financial institutions will have policies on exactly where and how
> money amounts are rounded, but it's unlikely you'll capture that just
> by relying on a Decimal type.


Also, consider using different types for different things,
e.g. Money for display, database tables, bank accounts, ... ,
and user defined floats or decimals for tax rates, and again
other (sub)types for interest rates and calculations reflecting
above mentioned policies.



--
Georg Bauhaus
Y A Time Drain  http://www.9toX.de



^ permalink raw reply	[flat|nested] 6+ messages in thread

end of thread, other threads:[~2008-07-21  8:30 UTC | newest]

Thread overview: 6+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2008-07-20 23:27 Financial Calculations in Ada rd
2008-07-21  2:32 ` Keith Thompson
2008-07-21  5:11   ` tmoran
2008-07-21  4:46 ` Gautier
2008-07-21  8:24 ` Graham Stark
2008-07-21  8:30   ` Georg Bauhaus

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox