comp.lang.ada
 help / color / mirror / Atom feed
* no + or - defined for fixed point types in Standard, why ?
@ 2018-01-24 17:03 Mehdi Saada
  2018-01-24 17:44 ` Robert Eachus
  2018-01-25  3:25 ` Randy Brukardt
  0 siblings, 2 replies; 9+ messages in thread
From: Mehdi Saada @ 2018-01-24 17:03 UTC (permalink / raw)


I have to define the addition, substraction and multiplication operators for a fixed point real type, and (though I KNOW you don't do, that, but pass use a parent type whose primitives have not been overrided) I tried to prefix by "Standard" to see what happens. I thought it would just not work.
I got that:
p_proba1.adb:11:37: warning: instance does not use primitive operation "+" at p_proba1.ads:8
p_proba1.adb:11:37: warning: instance does not use primitive operation "*" at p_proba1.ads:12
p_proba1.adb:11:37: warning: instance does not use primitive operation "-" at p_proba1.ads:16
p_proba1.adb:43:16: "-" not declared in "Standard"

I could see that + and - weren't defined for universal_fixed in Standard, though they are indeed, hum, defined in real life. What does it mean ?

Here's my type definitions:

   type T_proba is private;
private
   type Modele is delta 0.001 range 0.0..1.0 with Small => 0.001; -- to style have at hands primitives for fixed point types.
   type T_proba is new MODELE; -- what I know I should use (but it still say "instance blablabla"

And in the body, to define the operation, I convert to Modele.
But I got those warnings:
p_proba1.adb:11:37: warning: instance does not use primitive operation "+" at p_proba1.ads:8
p_proba1.adb:11:37: warning: instance does not use primitive operation "*" at p_proba1.ads:12
p_proba1.adb:11:37: warning: instance does not use primitive operation "-" at p_proba1.ads:16

Is there another way to refer to the T_proba's primitive operations ? And why does it upset the compiler ?
I try to add "overriding" to these three operations, since I read it was a good practice, and I only got one more error: "subprogram "*" is not overriding"...

I just read the course's parts on numerics, and I admit I didn't get most of it, by far. Was much much harder to me, than Ada proper. But I don't remember, and can't find info on any of these points...


My operators' definitions:
   function "*"
     (Un,                   
         Deux : in     T_Proba)
      return T_Proba is (Standard."*"(Un, Deux));
   function "+"
     (Un,                   
         Deux : in     T_Proba)
        return T_Proba is ((-Un) * (-Deux));
   function "-"
     (Un : in     T_Proba)
      return T_Proba
   is (Standard."-"(1.0 - UN));

ps: remember, I KNOW it's wrong. but I wonder why exactly.


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

* Re: no + or - defined for fixed point types in Standard, why ?
  2018-01-24 17:03 no + or - defined for fixed point types in Standard, why ? Mehdi Saada
@ 2018-01-24 17:44 ` Robert Eachus
  2018-01-24 18:36   ` Mehdi Saada
  2018-01-25  3:25 ` Randy Brukardt
  1 sibling, 1 reply; 9+ messages in thread
From: Robert Eachus @ 2018-01-24 17:44 UTC (permalink / raw)


On Wednesday, January 24, 2018 at 12:03:40 PM UTC-5, Mehdi Saada wrote:
> I have to define the addition, substraction and multiplication operators for a fixed point real type, and (though I KNOW you don't do, that, but pass use a parent type whose primitives have not been overrided) I tried to prefix by "Standard" to see what happens. I thought it would just not work.

The easy way to "get" Ada fixed point is that it is an integer type with a predefined scaling factor, 'Small.   The predefined operators include +, - multiplication or division by an integer (which returns a value of the type), and  the comparison operators which return Boolean.  Division of two values of the type, returning Integer could also be provided but isn't.

There are special multiplication and division operators which are defined for any two fixed point types, and return a value which must be converted to some type.  Note that division operations are the only ones that can result in rounding.  Obviously this is important when you want to minimize rounding.

As for your problem.the compiler is trying to be nice, and confusing you.  It is hard to correctly override the predefined operators where they are directly visible.  If you can do the overriding where they are use visible that works, except for the magic versions of multiply and divide (that do not have an integer parameter).  To (re-)define * or / you will need to write a body that uses Unchecked_Conversion of the proper integer type, or converts both operands to some floating point type.

As for the warnings?  You are trying to do something wonky, and the compiler is telling you that.  If you really want to do it, consider turning warnings off in the appropriate area.


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

* Re: no + or - defined for fixed point types in Standard, why ?
  2018-01-24 17:44 ` Robert Eachus
@ 2018-01-24 18:36   ` Mehdi Saada
  2018-01-25  1:09     ` Robert Eachus
  0 siblings, 1 reply; 9+ messages in thread
From: Mehdi Saada @ 2018-01-24 18:36 UTC (permalink / raw)


> It is hard to correctly override the predefined operators where they are directly visible.  [...]  To (re-)define * or / you will need to write a body that uses Unchecked_Conversion of the proper integer type, or converts both operands to some floating point type.
My gosh, did I stumbled against a language's corner again ? I think I'll stick with the conversion method... But can you elaborate on "directly visible" ? I don't understand. To me either it's visible, either it's not... 

Even with the conversion method, the warning about overridding won't go away. Eventhough there's no explicit call on Standard anymore.

> You are trying to do something wonky, and the compiler is telling you that.
I could tell before trying  ;-)


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

* Re: no + or - defined for fixed point types in Standard, why ?
  2018-01-24 18:36   ` Mehdi Saada
@ 2018-01-25  1:09     ` Robert Eachus
  2018-01-25  1:31       ` guyclaude.burger
  0 siblings, 1 reply; 9+ messages in thread
From: Robert Eachus @ 2018-01-25  1:09 UTC (permalink / raw)


On Wednesday, January 24, 2018 at 1:36:59 PM UTC-5, Mehdi Saada wrote:
 > I don't understand. To me either it's visible, either it's not... 
In Ada declarations are directly visible if they are declared in the current scope, or an enclosing scope.  Declarations are use visible if they are only visible due to a use clause.

A directly visible declaration hides any use visible declarations.  However, you can (almost always) use a qualified name to reference something which is use visible i.e. Ada.Text_IO.Put if there is another put declared locally that hides it.

About the almost always?  Don't do that!  The examples I used in testing almost always included a package Standard as a library unit...

> 
> Even with the conversion method, the warning about overridding won't go away. Eventhough there's no explicit call on Standard anymore.
> 
> > You are trying to do something wonky, and the compiler is telling you that.
> I could tell before trying  ;-)

As I said, you can turn off warnings if it bothers you.  I think if you look through the GNAT sources, there are a number of places where they do that.  As a programming practice, I like it.  It says that the programmer knows that the compiler won't like this, but I want to do it anyway.

A perfect example is a test to insure that a compiler raises Program_Error if a function "runs off the end" which is fine for procedures.

function Test is
begin if false then return; end if; end Test;

See what your compiler says about that.


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

* Re: no + or - defined for fixed point types in Standard, why ?
  2018-01-25  1:09     ` Robert Eachus
@ 2018-01-25  1:31       ` guyclaude.burger
  2018-01-25  3:07         ` Robert Eachus
  0 siblings, 1 reply; 9+ messages in thread
From: guyclaude.burger @ 2018-01-25  1:31 UTC (permalink / raw)


> As I said, you can turn off warnings if it bothers you.  I think if you look through the GNAT sources, there are a number of places where they do that.  As a programming practice, I like it.  It says that the programmer knows that the compiler won't like this, but I want to do it anyway.

I wouldn't have thought reading that from an Ada user 0_0
I don't think I'm nearly as wise as people who wrote the compiler. Even if I had been a qualifier programmer, I would think the same and abide by the conventions as much as I could. I'm too conservative in general to go against habits without due reasons or ways to do otherwise.

> function Test is
> begin if false then return; end if; end Test;
> 
> See what your compiler says about that.

it says the first line lacks a "return" statement, which even I can see... why writing such a thing ? Looks like a piece of C, with its void function, or whatever they call that. It's raising hairs on my shoulder, really.


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

* Re: no + or - defined for fixed point types in Standard, why ?
  2018-01-25  1:31       ` guyclaude.burger
@ 2018-01-25  3:07         ` Robert Eachus
  0 siblings, 0 replies; 9+ messages in thread
From: Robert Eachus @ 2018-01-25  3:07 UTC (permalink / raw)


On Wednesday, January 24, 2018 at 8:31:33 PM UTC-5, guyclaud...@gmail.com wrote:
> > As I said, you can turn off warnings if it bothers you.  I think if you look through the GNAT sources, there are a number of places where they do that.  As a programming practice, I like it.  It says that the programmer knows that the compiler won't like this, but I want to do it anyway.
> 
> I wouldn't have thought reading that from an Ada user 0_0
> I don't think I'm nearly as wise as people who wrote the compiler. Even if I had been a qualifier programmer, I would think the same and abide by the conventions as much as I could. I'm too conservative in general to go against habits without due reasons or ways to do otherwise.
> 
> > function Test is
> > begin if false then return; end if; end Test;
> >
> 
> it says the first line lacks a "return" statement, which even I can see... why writing such a thing ? Looks like a piece of C, with its void function, or whatever they call that. It's raising hairs on my shoulder, really.

 function Test return Integer is
 begin if false then return 6; end if; end Test;

Oops! wrong test case, try this:


 function Test3 return Integer is
 begin if false then return 6; end if; end Test3;

You should get at least one warning, and no errors.  Provide a Boolean parameter instead of false:

 function Test4 (X: Boolean) return Integer is
 begin if X then return 6; end if; end Test4;

And you should be down to one warning.

Compiler writers have thousands of these ugly things and sometimes what the compiler guesses you intended is good for a laugh.  Some test cases are regression tests collected from bug reports--you never want to reintroduce an old bug.  Others are designed to thoroughly test all the ways you imagine that a new feature can get misused. 

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

* Re: no + or - defined for fixed point types in Standard, why ?
  2018-01-24 17:03 no + or - defined for fixed point types in Standard, why ? Mehdi Saada
  2018-01-24 17:44 ` Robert Eachus
@ 2018-01-25  3:25 ` Randy Brukardt
  2018-01-25 13:33   ` Mehdi Saada
  1 sibling, 1 reply; 9+ messages in thread
From: Randy Brukardt @ 2018-01-25  3:25 UTC (permalink / raw)


"+" and "-" (and many other operators, as well, look in Chapter 4 of the Ada 
Standard) are declared at the point of the type declaration, not in package 
Standard. So if you need to use direct notation for the ones in this 
program, you have to prefix by the package name that contains the operators 
(which you didn't provide because you didn't provide a compilable example 
yet again), not Standard.

Ignore the goofy "*" and "/" for fixed point types in Standard, they have a 
specialized use. The usual operators are declared with the type, just like 
with integer and float types. Most of the operators in Standard are for use 
with the predefined types, which a type with an explicit declaration like 
Modele is not.

Derived types get a copy of the operators of the parent type, also declared 
at the point of the type. So the same is true for T_Proba.

                        Randy.


"Mehdi Saada" <00120260a@gmail.com> wrote in message 
news:3d796e5f-015e-469c-bbcb-edc3303793ab@googlegroups.com...
I have to define the addition, substraction and multiplication operators for 
a fixed point real type, and (though I KNOW you don't do, that, but pass use 
a parent type whose primitives have not been overrided) I tried to prefix by 
"Standard" to see what happens. I thought it would just not work.
I got that:
p_proba1.adb:11:37: warning: instance does not use primitive operation "+" 
at p_proba1.ads:8
p_proba1.adb:11:37: warning: instance does not use primitive operation "*" 
at p_proba1.ads:12
p_proba1.adb:11:37: warning: instance does not use primitive operation "-" 
at p_proba1.ads:16
p_proba1.adb:43:16: "-" not declared in "Standard"

I could see that + and - weren't defined for universal_fixed in Standard, 
though they are indeed, hum, defined in real life. What does it mean ?

Here's my type definitions:

   type T_proba is private;
private
   type Modele is delta 0.001 range 0.0..1.0 with Small => 0.001; -- to 
style have at hands primitives for fixed point types.
   type T_proba is new MODELE; -- what I know I should use (but it still say 
"instance blablabla"

And in the body, to define the operation, I convert to Modele.
But I got those warnings:
p_proba1.adb:11:37: warning: instance does not use primitive operation "+" 
at p_proba1.ads:8
p_proba1.adb:11:37: warning: instance does not use primitive operation "*" 
at p_proba1.ads:12
p_proba1.adb:11:37: warning: instance does not use primitive operation "-" 
at p_proba1.ads:16

Is there another way to refer to the T_proba's primitive operations ? And 
why does it upset the compiler ?
I try to add "overriding" to these three operations, since I read it was a 
good practice, and I only got one more error: "subprogram "*" is not 
overriding"...

I just read the course's parts on numerics, and I admit I didn't get most of 
it, by far. Was much much harder to me, than Ada proper. But I don't 
remember, and can't find info on any of these points...


My operators' definitions:
   function "*"
     (Un,
         Deux : in     T_Proba)
      return T_Proba is (Standard."*"(Un, Deux));
   function "+"
     (Un,
         Deux : in     T_Proba)
        return T_Proba is ((-Un) * (-Deux));
   function "-"
     (Un : in     T_Proba)
      return T_Proba
   is (Standard."-"(1.0 - UN));

ps: remember, I KNOW it's wrong. but I wonder why exactly. 



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

* Re: no + or - defined for fixed point types in Standard, why ?
  2018-01-25  3:25 ` Randy Brukardt
@ 2018-01-25 13:33   ` Mehdi Saada
  2018-01-26  4:34     ` Randy Brukardt
  0 siblings, 1 reply; 9+ messages in thread
From: Mehdi Saada @ 2018-01-25 13:33 UTC (permalink / raw)


generic
   type Modele is delta <>;
package P_Proba1 is
   type T_Proba is private; 
   function "+" (
         Un,                   
         Deux : in     T_Proba ) 
     return T_Proba; 
private
	type T_proba is new MODELE range 0.1..1.0;
end P_Proba1;

package body P_Proba1 is
	function "+"
     (Un,                   
         Deux : in     T_Proba)
        return T_Proba is (P_Proba1."+"(UN,Deux));
end P_Proba1;

> program, you have to prefix by the package name that contains the operators : My code.
It causes infinite recursion (dixit compiler), so I didn't understand.


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

* Re: no + or - defined for fixed point types in Standard, why ?
  2018-01-25 13:33   ` Mehdi Saada
@ 2018-01-26  4:34     ` Randy Brukardt
  0 siblings, 0 replies; 9+ messages in thread
From: Randy Brukardt @ 2018-01-26  4:34 UTC (permalink / raw)


"Mehdi Saada" <00120260a@gmail.com> wrote in message 
news:3c342468-67da-4973-a318-14e0cf1293fb@googlegroups.com...
> generic
>   type Modele is delta <>;
> package P_Proba1 is
>   type T_Proba is private;
>   function "+" (
>         Un,
>         Deux : in     T_Proba )
>     return T_Proba;
> private
> type T_proba is new MODELE range 0.1..1.0;
> end P_Proba1;
>
> package body P_Proba1 is
> function "+"
>     (Un,
>         Deux : in     T_Proba)
>        return T_Proba is (P_Proba1."+"(UN,Deux));
> end P_Proba1;
>
>> program, you have to prefix by the package name that contains the 
>> operators : My code.
> It causes infinite recursion (dixit compiler), so I didn't understand.

Yup, that calls the operator you just defined, and not the original one. In 
this case, there is no way to get to the original operator of T_Proba; 
you've hidden it with the new one. (This is a common Ada programming mistake 
when defining operators, happens to me all the time.) You have to use some 
other type's "+" operator with type conversions. In this case, you might as 
well use the operators of Modele (which are declared with it at the formal 
type declaration):

T_Proba (Modele (Un) + Modele (Deux));

There's a way to get at the original operator using renames, but it isn't 
worth the headache unless you don't have a similar type that you can convert 
to.

                                   Randy.



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

end of thread, other threads:[~2018-01-26  4:34 UTC | newest]

Thread overview: 9+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2018-01-24 17:03 no + or - defined for fixed point types in Standard, why ? Mehdi Saada
2018-01-24 17:44 ` Robert Eachus
2018-01-24 18:36   ` Mehdi Saada
2018-01-25  1:09     ` Robert Eachus
2018-01-25  1:31       ` guyclaude.burger
2018-01-25  3:07         ` Robert Eachus
2018-01-25  3:25 ` Randy Brukardt
2018-01-25 13:33   ` Mehdi Saada
2018-01-26  4:34     ` Randy Brukardt

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