Averages/Mean angle (Rosetta Code) run-time error

Hi;

I get a run-time error when exploring the Ada Rosetta Code Task:

“Averages/Mean angle”

gnatmake ./mean_angles.adb
gcc -c -I./ -I- ./mean_angles.adb
gnatbind -x mean_angles.ali
gnatlink mean_angles.ali
Averages_Mean_Angle: ./mean_angles
20.00
0.00

raised ADA.NUMERICS.ARGUMENT_ERROR : a-ngelfu.adb:424 instantiated at mean_angles.adb:20

I’m running on MacOS, M1 chip, with the gnat compiler from Simon (14.2.0-3-aarch64)

When running under Alire:

Execution of ./bin/mean_angles terminated by unhandled exception
raised ADA.NUMERICS.ARGUMENT_ERROR : a-ngelfu.adb:424 instantiated at mean_angles.adb:20
Load address: 0x104074000
Call stack traceback locations:
0x10407975c 0x1040797bc 0x104076894 0x104077078 0x10407752c 0x104097ec4

Is the Ada code on the Rosetta Code web site incorrect?

I’ve tried it using different gnat version flags (-gnat2005, -gnat95, etc)…

Thanks,
Retired Build Engineer

Hi there,

Here is a theory, of what may be happening…

In the third call to the Mean_Angle subprogram, with the parameters:
(90.0, 180.0, 270.0, 360,0), the sum of SIN and COS components are both zero. This results in calling the Arctan subprogram with X = 0.0 and Y = 0.0.

As per Ada Reference Manual (ARM) A.5.1(25), the implementation raises an Argument_Error because X and Y (the cycle parameters) are both zero.

Just for reference, here’s an implementation of Elementary_Functions I found online: drake/source/numerics/a-ngelfu.adb at 4e4bdcd8b8e23a11a29b31d3a8861fdf60090ea2 · ytomino/drake · GitHub

Look at the implementation for Arctan.

N

Hi;

I suspected that was the case, but wasn’t sure enough to even write down my guess in my post here.

So, I could add value to this Rosetta Code example by either not processing arguments that are zero (or close to zero) by stating such; or I could write an exception handler that would exit more nicely.

Thanks,
RBE

You could correct the comment

        -- may raise Ada.Numerics.Argument_Error if inputs are
        -- numerically instable, e.g., when Cos_Sum is 0.0

to say that it will raise Argument_Error if the arguments are both zero (of course it will give a less-than-useful result if the arguments are both very small).

Also, it’d be better to run the examples in the order presented in the problem!

Hi Simon;

Thank you for additional suggestions. When I update the Rosetta Code entry (I was not the original author), I’ll use the input data in the code as described in the task description.

Thanks,
RBE