comp.lang.ada
 help / color / mirror / Atom feed
* Rounding with decimal types
@ 2007-11-25 15:36 Wiktor Moskwa
       [not found] ` <13kjevsjmhujaa3@corp.supernews.com>
  0 siblings, 1 reply; 6+ messages in thread
From: Wiktor Moskwa @ 2007-11-25 15:36 UTC (permalink / raw)


Hello,

I'm trying to understand how rounding in Ada 95 decimal types works.
Reference manual is unfortunately not clear to me on this subject.
I declare a decimal type and few variables:

type My_Decimal is delta 0.01 digits 18;
A, B, C : My_Decimal;

I want to have full control over rounding after multiplication and
division. Let me post two examples:

A := 0.01;
B := 0.50;
C := My_Decimal (A * B);	-- now C = 0.00

A := 0.01;
B := 0.50;
C := My_Decimal'Round (A * B);	-- now C = 0.01

So in the former example the result is truncated towards 0 and
in the latter it is rounded away from 0 when the result lies
between two integers. 
That's OK, but is it all that I can do with decimal numbers? 
How can I round up, down (explicitly) or perform "banker's rounding"?
Unfortunately 'Floor 'Ceiling and 'Unbiased_Rounding seem to work only
for floating point types.

The problem that I'm trying to solve is what type is best suited
for financial arithmetic and money representation.

Thanks for your help.

-- 
Wiktor Moskwa



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

* Re: Rounding with decimal types
       [not found] ` <13kjevsjmhujaa3@corp.supernews.com>
@ 2007-11-26  8:15   ` Wiktor Moskwa
  2007-11-26 13:47     ` Dmitry A. Kazakov
  0 siblings, 1 reply; 6+ messages in thread
From: Wiktor Moskwa @ 2007-11-26  8:15 UTC (permalink / raw)


On 25.11.2007, Dennis Lee Bieber <wlfraed@ix.netcom.com> wrote:
> On Sun, 25 Nov 2007 15:36:39 +0000 (UTC), Wiktor Moskwa
><wiktorDOTmoskwa@gmail.com> declaimed the following in comp.lang.ada:
>
>> The problem that I'm trying to solve is what type is best suited
>> for financial arithmetic and money representation.
>>
> 	For this, I'd suggest you define your data type to carry at least
> one decimal place more significance than the monetary unit normally
> supports. That gives you a guard digit on which to apply your own
> rounding algorithm for display (you should probably NOT round values
> that will be used for continued computation -- only round the display
> representation).

Hi Dennis,

Thanks for your hint but I'm only trying to use a tool which is decimal type
in Ada. Guard digit and extended precision would be a good idea if we needed
the best possible accuracy but if the process of computation is well defined 
including what method of rounding to use in which step, extra precision 
would be erroneus.

I'd like to know if there is "round up" and "unbiased rounding"
implemented for decimal types in Ada and if not, how "is it usually done"?
Thank you.

-- 
Wiktor Moskwa



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

* Rounding with decimal types
@ 2007-11-26 12:27 Grein, Christoph (Fa. ESG)
  2007-11-26 14:46 ` Wiktor Moskwa
  0 siblings, 1 reply; 6+ messages in thread
From: Grein, Christoph (Fa. ESG) @ 2007-11-26 12:27 UTC (permalink / raw)
  To: comp.lang.ada

For some information about decimal fixed point types and their
differences to ordinary fixed point types, see

http://en.wikibooks.org/wiki/Ada_Programming/Types/delta


Eurocopter Deutschland GmbH
Sitz der Gesellschaft/Registered Office: Donauwoerth
Registergericht/Registration Court: Amtsgericht Augsburg HRB 16508
Vorsitzender des Aufsichtsrates/Chairman of the Supervisory Board: Dr. Lutz Bertling
Geschaeftsfuehrung/Board of Management:
Dr. Wolfgang Schoder, Vorsitzender/CEO; Friedrich-Wilhelm Hormel; Ralf Barnscheidt

CONFIDENTIALITY NOTICE 

This communication and the information it contains is intended for the addressee(s) named above and for no other persons or organizations. It is confidential and may be legally privileged and protected by law. The unauthorized use, copying or disclosure of this communication or any part of it is prohibited and may be unlawful. 
If you have received this communication in error, kindly notify us by return e-mail and discard and/or delete the communication. Thank you very much. 
It is possible for e-mails to be intercepted or affected by viruses. Whilst we maintain virus checks on our e-mails, we accept no liability for viruses or other material which might be introduced with this message. 




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

* Re: Rounding with decimal types
  2007-11-26  8:15   ` Wiktor Moskwa
@ 2007-11-26 13:47     ` Dmitry A. Kazakov
  2007-11-26 14:55       ` Wiktor Moskwa
  0 siblings, 1 reply; 6+ messages in thread
From: Dmitry A. Kazakov @ 2007-11-26 13:47 UTC (permalink / raw)


On Mon, 26 Nov 2007 08:15:02 +0000 (UTC), Wiktor Moskwa wrote:

> I'd like to know if there is "round up" and "unbiased rounding"
> implemented for decimal types in Ada and if not, how "is it usually done"?

Yep, this is a problem, I faced it too but for floating-point numbers when
implemented interval computations. There one needs rounding up and down as
well.

I would suggest something as clumsy as:

   -- Up-rounding multiplication
function "*" (Left, Right : My_Decimal) return My_Decimal is
   type Impl is new My_Decimal;
begin
   if Left = 0.0 then
      return 0.0;
   else
      declare
         Result : Impl := Impl'(Impl (Left) * Impl (Right));
      begin
         if Result / Left /= Right then
            return My_Decimal'Succ (My_Decimal (Result));
         else
            return My_Decimal (Result);
         end if;
      end;
   end if;
end "*";

-- 
Regards,
Dmitry A. Kazakov
http://www.dmitry-kazakov.de



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

* Re: Rounding with decimal types
  2007-11-26 12:27 Rounding with decimal types Grein, Christoph (Fa. ESG)
@ 2007-11-26 14:46 ` Wiktor Moskwa
  0 siblings, 0 replies; 6+ messages in thread
From: Wiktor Moskwa @ 2007-11-26 14:46 UTC (permalink / raw)


On 26.11.2007, Grein, Christoph (Fa. ESG) <Christoph.Grein@eurocopter.com> wrote:
> For some information about decimal fixed point types and their
> differences to ordinary fixed point types, see
>
> http://en.wikibooks.org/wiki/Ada_Programming/Types/delta
>

This page of Ada wikibook was one of the first places that I visited :-)
But thanks anyway.

-- 
Wiktor Moskwa



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

* Re: Rounding with decimal types
  2007-11-26 13:47     ` Dmitry A. Kazakov
@ 2007-11-26 14:55       ` Wiktor Moskwa
  0 siblings, 0 replies; 6+ messages in thread
From: Wiktor Moskwa @ 2007-11-26 14:55 UTC (permalink / raw)


On 26.11.2007, Dmitry A. Kazakov <mailbox@dmitry-kazakov.de> wrote:
> On Mon, 26 Nov 2007 08:15:02 +0000 (UTC), Wiktor Moskwa wrote:
>
>> I'd like to know if there is "round up" and "unbiased rounding"
>> implemented for decimal types in Ada and if not, how "is it usually done"?
>
> Yep, this is a problem, I faced it too but for floating-point numbers when
> implemented interval computations. There one needs rounding up and down as
> well.
>
> I would suggest something as clumsy as:
> [..]

I've almost forgotten about operator overloading in Ada (too much
Java coding...) - thanks for reminding :)

-- 
Wiktor Moskwa



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

end of thread, other threads:[~2007-11-26 14:55 UTC | newest]

Thread overview: 6+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2007-11-26 12:27 Rounding with decimal types Grein, Christoph (Fa. ESG)
2007-11-26 14:46 ` Wiktor Moskwa
  -- strict thread matches above, loose matches on Subject: below --
2007-11-25 15:36 Wiktor Moskwa
     [not found] ` <13kjevsjmhujaa3@corp.supernews.com>
2007-11-26  8:15   ` Wiktor Moskwa
2007-11-26 13:47     ` Dmitry A. Kazakov
2007-11-26 14:55       ` Wiktor Moskwa

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