Pregunta

Uso Ada (GNAT): Necesito para determinar la potencia de diez para un valor dado. El método más obvio es el uso de un logaritmo; pero que falla al compilar.

with Ada.Numerics.Generic_Elementary_Functions;
procedure F(Value : in Float) is
      The_Log : Integer := 0;
begin
      The_Log := Integer(Log(Value, 10));
      G(Value, The_Log);
end;

Error:

  • utilities.adb: 495: 26: "registro" no es visible
    • utilities.adb: 495: 26: declaración no visible en a-ngelfu.ads:24, instancia en la línea 482
    • utilities.adb: 495: 26: declaración no visible en a-ngelfu.ads:23, instancia en la línea 482

Así que intento hacer referencia al paquete, pero que también falla:

with Ada.Numerics.Generic_Elementary_Functions;
procedure F(Value : in Float) is
      The_Log : Integer := 0;
      package Float_Functions is new Ada.Numerics.Generic_Elementary_Functions (Float);
begin
      The_Log := Integer(Float_Functions.Log(Value, 10));
      G(Value, The_Log);
end;

Error:

  • utilities.adb: 495: 41: no hay interpretaciones candidatos coinciden con los datos reales:
  • utilities.adb: 495: 41: demasiados argumentos en la llamada a "Log"
  • utilities.adb: 495: 53: tipo esperado "Standard.Float"
  • utilities.adb: 495: 53: encontrado tipo entero universales ==> en la llamada a "Log" en a-ngelfu.ads:24, instancia en la línea 482
¿Fue útil?

Solución

No sé si te fijas ya o no, pero aquí está la respuesta.

En primer lugar, como veo que está pasando Float cuando una instancia de la versión genérica que puede usar el que no genérico en lugar.

Si decide utilizar la versión genérica que tiene que hacer es la segunda manera que lo hizo, usted tiene que crear una instancia del paquete antes de utilizar sus funciones.

En cuanto a a-ngelfu.ads es posible que vea el prototipo real de la función que necesita (hay otra función para el logaritmo natural con sólo 1 de parámetros):

function Log(X, Base : Float_Type'Base) return Float_Type'Base;

Se puede ver que la base no tiene que estar en un tipo de flotador también. El código correcto para la versión genérica sería:

with Ada.Numerics.Generic_Elementary_Functions;

procedure F(Value : in Float) is
    -- Instantiate the package:
    package Float_Functions is new Ada.Numerics.Generic_Elementary_Functions (Float);
    -- The logarithm:
    The_Log : Integer := 0;
begin
    The_Log := Integer(Float_Functions.Log(Value, 10.0));
    G(Value, The_Log);
end;

El que no genérico sería exactamente el mismo:

with Ada.Numerics.Elementary_Functions;

procedure F(Value : in Float) is
    -- The logarithm:
    The_Log : Integer := 0;
begin
    The_Log := Integer(Ada.Numerics.Elementary_Functions.Log(Value, 10.0));
    G(Value, The_Log);
end;

Otros consejos

Xandy es correcto. Su solución funcionó.

No obstante ser Ada había dos excepciones para protegerse contra ...

  • valor <0,0
  • Valor = 0,0

Sin guardias de esta función ya que es causa excepciones que se generen. Y recuerda The_Log devuelto puede ser <0, 0, y> 0.

with Ada.Numerics.Elementary_Functions; 

procedure F(Value : in Float) is 
    -- The logarithm: 
    The_Log : Integer := 0; 
begin 
    if Value /= 0.0 then
        The_Log := Integer(
             Ada.Numerics.Elementary_Functions.Log(abs Value, 10.0)); 
    end if;
    G(Value, The_Log); 
end; 
Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top