comp.lang.ada
 help / color / mirror / Atom feed
* Why raises this an error? :-(
@ 2002-03-16 19:48 Kai Gläsner
  2002-03-16 20:32 ` Damien Carbonne
                   ` (2 more replies)
  0 siblings, 3 replies; 9+ messages in thread
From: Kai Gläsner @ 2002-03-16 19:48 UTC (permalink / raw)


Hello community,

while porting an c++ math-library to Ada95 (Gnat 3.12p under Linux) I
experienced:

This:

    Alt_D := (-8.0/4.0) ** 2.0;

raises this:

    raised ADA.NUMERICS.ARGUMENT_ERROR : a-ngelfu.adb:111 instantiated
at a-nuelfu.ads:20

This:

    Alt_D := -2.0 ** 2.0;

works fine!

Why? What have I overlooked here... :-/

Thanks in advance for an answer...

Kai Glaesner



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

* Re: Why raises this an error? :-(
  2002-03-16 19:48 Why raises this an error? :-( Kai Gläsner
@ 2002-03-16 20:32 ` Damien Carbonne
  2002-03-17 16:31   ` Steve Doiel
  2002-03-16 20:37 ` Jeffrey Carter
  2002-03-16 22:12 ` Kai Gläsner
  2 siblings, 1 reply; 9+ messages in thread
From: Damien Carbonne @ 2002-03-16 20:32 UTC (permalink / raw)


Kai Gl�sner a �crit :

> Hello community,
>
> while porting an c++ math-library to Ada95 (Gnat 3.12p under Linux) I
> experienced:
>
> This:
>
>     Alt_D := (-8.0/4.0) ** 2.0;
>
> raises this:
>
>     raised ADA.NUMERICS.ARGUMENT_ERROR : a-ngelfu.adb:111 instantiated
> at a-nuelfu.ads:20
>
> This:
>
>     Alt_D := -2.0 ** 2.0;
>
> works fine!
>
> Why? What have I overlooked here... :-/
>
> Thanks in advance for an answer...
>
> Kai Glaesner

I am not completely sure, but there may be a precedence issue here.
I think -2.0**2.0 is equivalent to -(2.0**2.0)
whilst (-8.0/4.0)**2.0 is equivalent to (-2.0)**2.0.
There is no general interpretation to the power of a negative number (except
when the power is an integer value)
Anyway, if what you need is the squarre value of a number, I think a simple
multiplication is much faster than a general exponentiation algorithm.

Regards

Damien carbonne




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

* Re: Why raises this an error? :-(
  2002-03-16 19:48 Why raises this an error? :-( Kai Gläsner
  2002-03-16 20:32 ` Damien Carbonne
@ 2002-03-16 20:37 ` Jeffrey Carter
  2002-03-16 22:01   ` Kai Gläsner
  2002-03-16 22:12 ` Kai Gläsner
  2 siblings, 1 reply; 9+ messages in thread
From: Jeffrey Carter @ 2002-03-16 20:37 UTC (permalink / raw)


Kai Gl�sner wrote:
> 
> This:
> 
>     Alt_D := (-8.0/4.0) ** 2.0;
> 
> raises this:
> 
>     raised ADA.NUMERICS.ARGUMENT_ERROR : a-ngelfu.adb:111 instantiated
> at a-nuelfu.ads:20
> 
> This:
> 
>     Alt_D := -2.0 ** 2.0;
> 
> works fine!

Exponentiation has higher precedence than unary minus (ARM 4.5), so this
is equivalent to

Alt_D := -(2.0 ** 2.0);

ARM A.5.1 says that Argument_Error is raised "by the exponentiation
operator, when the value of the left operand is negative". This is the
case in your first example.

Since your exponent is an integer value, you can avoid this restriction
by using the predefined exponentiation operator for floating-point
types:

Alt_D := (-8.0 / 4.0) ** 2;

-- 
Jeff Carter
"Your mother was a hamster and your father smelt of elderberries."
Monty Python & the Holy Grail



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

* Re: Why raises this an error? :-(
  2002-03-16 20:37 ` Jeffrey Carter
@ 2002-03-16 22:01   ` Kai Gläsner
  2002-03-17  5:39     ` Jeffrey Carter
  0 siblings, 1 reply; 9+ messages in thread
From: Kai Gläsner @ 2002-03-16 22:01 UTC (permalink / raw)


Thanks for the quick Response.

The problem I have is, that the exponent is not an Integer (this was
just a test-example to find out what is wrong)

   <snip> 1.0 - (GetTemperature(The, Alt)/Temp) ** 0.2349690

Where GetTemperature() can return a value < 0.0.

But why is the value of the left operand not allowed to be negative
according to the ARM?

Jeffrey Carter wrote:
> 
> Kai Gl�sner wrote:
> >
> > This:
> >
> >     Alt_D := (-8.0/4.0) ** 2.0;
> >
> > raises this:
> >
> >     raised ADA.NUMERICS.ARGUMENT_ERROR : a-ngelfu.adb:111 instantiated
> > at a-nuelfu.ads:20
> >
> > This:
> >
> >     Alt_D := -2.0 ** 2.0;
> >
> > works fine!
> 
> Exponentiation has higher precedence than unary minus (ARM 4.5), so this
> is equivalent to
> 
> Alt_D := -(2.0 ** 2.0);
> 
> ARM A.5.1 says that Argument_Error is raised "by the exponentiation
> operator, when the value of the left operand is negative". This is the
> case in your first example.
> 
> Since your exponent is an integer value, you can avoid this restriction
> by using the predefined exponentiation operator for floating-point
> types:
> 
> Alt_D := (-8.0 / 4.0) ** 2;
> 
> --
> Jeff Carter
> "Your mother was a hamster and your father smelt of elderberries."
> Monty Python & the Holy Grail



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

* Re: Why raises this an error? :-(
  2002-03-16 19:48 Why raises this an error? :-( Kai Gläsner
  2002-03-16 20:32 ` Damien Carbonne
  2002-03-16 20:37 ` Jeffrey Carter
@ 2002-03-16 22:12 ` Kai Gläsner
  2002-03-17  1:06   ` tmoran
  2 siblings, 1 reply; 9+ messages in thread
From: Kai Gläsner @ 2002-03-16 22:12 UTC (permalink / raw)


...seems I ran into an mathematical absolute here...

Found a workaround by converting all temperatures to Kelvin (no negative
values there ;-)

Thanks for the support...

Kai Glaesner



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

* Re: Why raises this an error? :-(
  2002-03-16 22:12 ` Kai Gläsner
@ 2002-03-17  1:06   ` tmoran
  0 siblings, 0 replies; 9+ messages in thread
From: tmoran @ 2002-03-17  1:06 UTC (permalink / raw)


>  <snip> 1.0 - (GetTemperature(The, Alt)/Temp) ** 0.2349690
  And what's supposed to be the value on cold days when Temp = 0 C or F?
This is unlikely to make any physical sense.

>Found a workaround by converting all temperatures to Kelvin (no negative
  A really nice example of the compiler forcing you to go back and
reconsider "did I *really* want to do that?" and changing to a better way.

> > >     Alt_D := (-8.0/4.0) ** 2.0;
> > The problem I have is, that the exponent is not an Integer (this was
> But why is the value of the left operand not allowed to be negative
> according to the ARM?
  What is the value of (-2.0)**2.01?  Is that really what you wanted?



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

* Re: Why raises this an error? :-(
  2002-03-16 22:01   ` Kai Gläsner
@ 2002-03-17  5:39     ` Jeffrey Carter
  2002-03-17  7:25       ` Hyman Rosen
  0 siblings, 1 reply; 9+ messages in thread
From: Jeffrey Carter @ 2002-03-17  5:39 UTC (permalink / raw)


Kai Gl�sner wrote:
> 
> But why is the value of the left operand not allowed to be negative
> according to the ARM?

Both the ARM and the AARM are silent on this. A possible reason is that
X ** Y may be implemented as

exp (log (X) * Y)

and you cannot take the log of a negative value. This is simply a guess
on my part, though.

-- 
Jeff Carter
"Your mother was a hamster and your father smelt of elderberries."
Monty Python & the Holy Grail



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

* Re: Why raises this an error? :-(
  2002-03-17  5:39     ` Jeffrey Carter
@ 2002-03-17  7:25       ` Hyman Rosen
  0 siblings, 0 replies; 9+ messages in thread
From: Hyman Rosen @ 2002-03-17  7:25 UTC (permalink / raw)


Jeffrey Carter wrote:
> Both the ARM and the AARM are silent on this.

Well, it's simple enough, isn't it? Negative numbers
raised to non-integer powers give complex, not real,
results. Just consider (-1) ** 0.5.




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

* Re: Why raises this an error? :-(
  2002-03-16 20:32 ` Damien Carbonne
@ 2002-03-17 16:31   ` Steve Doiel
  0 siblings, 0 replies; 9+ messages in thread
From: Steve Doiel @ 2002-03-17 16:31 UTC (permalink / raw)



"Damien Carbonne" <damien.carbonne@free.fr> wrote in message
news:3C93ABC4.C27E9D5F@free.fr...
[snip]
> Anyway, if what you need is the squarre value of a number, I think a
simple
> multiplication is much faster than a general exponentiation algorithm.
>

In general I think it's better to let the compiler make that decision.

SteveD

> Regards
>
> Damien carbonne
>





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

end of thread, other threads:[~2002-03-17 16:31 UTC | newest]

Thread overview: 9+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2002-03-16 19:48 Why raises this an error? :-( Kai Gläsner
2002-03-16 20:32 ` Damien Carbonne
2002-03-17 16:31   ` Steve Doiel
2002-03-16 20:37 ` Jeffrey Carter
2002-03-16 22:01   ` Kai Gläsner
2002-03-17  5:39     ` Jeffrey Carter
2002-03-17  7:25       ` Hyman Rosen
2002-03-16 22:12 ` Kai Gläsner
2002-03-17  1:06   ` tmoran

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