comp.lang.ada
 help / color / mirror / Atom feed
* Integer? float? confused...
@ 2001-03-20 21:31 WM
  2001-03-23  7:07 ` Sven Nilsson
  0 siblings, 1 reply; 9+ messages in thread
From: WM @ 2001-03-20 21:31 UTC (permalink / raw)


I have written a program to calculate Sin. The compiler(AdaGIDE 6.23) said:
"expected type standard.float", why? Please piont out errors, Thanks.

with Ada.Text_Io, Ada.Integer_Text_Io,Ada.Float_Text_Io;
use Ada.Text_Io, Ada.Integer_Text_Io,Ada.Float_Text_Io;

procedure sin is
   sin_result : float;
   i : integer;
   function Calc_Sin (Num : Integer) return float is
      begin
         Sin_Result := Num - (Num ** 3) / 6 + (Num ** 5) / 120;
         return Sin_Result;
   end Calc_Sin;

begin

put("input int: ");
get(i);
Sin_Result := Calc_Sin(Num=>i);
Put(Item=>Sin_Result, Fore=>1, Aft=>4, Exp=>0);

end sin;





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

* Integer? float? confused...
@ 2001-03-20 21:31 WM
  2001-03-20 22:55 ` chris.danx
                   ` (3 more replies)
  0 siblings, 4 replies; 9+ messages in thread
From: WM @ 2001-03-20 21:31 UTC (permalink / raw)


I have written a program to calculate Sin. The compiler(AdaGIDE 6.23) said:
"expected type standard.float", why? Please piont out errors, Thanks.

with Ada.Text_Io, Ada.Integer_Text_Io,Ada.Float_Text_Io;
use Ada.Text_Io, Ada.Integer_Text_Io,Ada.Float_Text_Io;

procedure sin is
   sin_result : float;
   i : integer;
   function Calc_Sin (Num : Integer) return float is
      begin
         Sin_Result := Num - (Num ** 3) / 6 + (Num ** 5) / 120;
         return Sin_Result;
   end Calc_Sin;

begin

put("input int: ");
get(i);
Sin_Result := Calc_Sin(Num=>i);
Put(Item=>Sin_Result, Fore=>1, Aft=>4, Exp=>0);

end sin;





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

* Integer? float? confused...
@ 2001-03-20 21:31 WM
  2001-04-05 18:30 ` mark
  0 siblings, 1 reply; 9+ messages in thread
From: WM @ 2001-03-20 21:31 UTC (permalink / raw)


I have written a program to calculate Sin. The compiler(AdaGIDE 6.23) said:
"expected type standard.float", why? Please piont out errors, Thanks.

with Ada.Text_Io, Ada.Integer_Text_Io,Ada.Float_Text_Io;
use Ada.Text_Io, Ada.Integer_Text_Io,Ada.Float_Text_Io;

procedure sin is
   sin_result : float;
   i : integer;
   function Calc_Sin (Num : Integer) return float is
      begin
         Sin_Result := Num - (Num ** 3) / 6 + (Num ** 5) / 120;
         return Sin_Result;
   end Calc_Sin;

begin

put("input int: ");
get(i);
Sin_Result := Calc_Sin(Num=>i);
Put(Item=>Sin_Result, Fore=>1, Aft=>4, Exp=>0);

end sin;





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

* Re: Integer? float? confused...
  2001-03-20 21:31 WM
@ 2001-03-20 22:55 ` chris.danx
  2001-03-20 22:59 ` EtienneB
                   ` (2 subsequent siblings)
  3 siblings, 0 replies; 9+ messages in thread
From: chris.danx @ 2001-03-20 22:55 UTC (permalink / raw)


You need to use 3.0, 6.0, 5.0 and 120.0 in function Calc_Sin.  This is
because Ada requires a float to have decimal point in it somewhere.  If you
don't include the .0 Ada thinks it's an integer.  Also convert the Num to a
float by Float(Num) otherwise Ada thinks Num is an integer -- which it is!
but your can't mix types so you've got to convert.

May i make a suggestion?  It would be better to have

function Calc_Sin (Num : integer) return float is
begin
    return Num - (Num ** 3) / 6 + (Num ** 5) / 120;
end Calc_Sin

instead of using sin_result.  This variable is global and in other programs
you might do something similar and it'll screw everything up.  You could
move the declaration of Sin_Result inside the function and what you've
written would work if you fixed the type conversion problem.  I haven't
fixed it in the above function cos' that's for you to do.



Hope this helps,
Chris Campbell

p.s. let me know *when* you get it working, or if anythings not clear -- i'm
terrible at explaining things to people.






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

* Re: Integer? float? confused...
  2001-03-20 21:31 WM
  2001-03-20 22:55 ` chris.danx
@ 2001-03-20 22:59 ` EtienneB
  2001-03-21  1:32 ` Ken Garlington
  2001-03-23 12:08 ` dis00109
  3 siblings, 0 replies; 9+ messages in thread
From: EtienneB @ 2001-03-20 22:59 UTC (permalink / raw)



>          Sin_Result := Num - (Num ** 3) / 6 + (Num ** 5) / 120;
>          return Sin_Result;

Num - (Num ** 3) / 6 + (Num ** 5) / 120;
this expression is integer and your function must return float
    I advice you to change num type to float and to return

Num - (Num ** 3) / 6.0 + (Num ** 5) / 120.0;

But sin(x) is already defined in ada.numerics.aux
Voila
Etienne Baudin






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

* Re: Integer? float? confused...
  2001-03-20 21:31 WM
  2001-03-20 22:55 ` chris.danx
  2001-03-20 22:59 ` EtienneB
@ 2001-03-21  1:32 ` Ken Garlington
  2001-03-23 12:08 ` dis00109
  3 siblings, 0 replies; 9+ messages in thread
From: Ken Garlington @ 2001-03-21  1:32 UTC (permalink / raw)


I ran this program through AdaGIDE v 6.26, and it gave me one other piece of
information: the line and column number where the error occurred. (I'm
pretty sure that AdaGIDE v 6.23 does the same.) I've annotated your source
code, see below...

"WM" <wwminirl@hotmail.com> wrote in message
news:998i7d$19j21@tech.port.ac.uk...
: I have written a program to calculate Sin. The compiler(AdaGIDE 6.23)
said:
: "expected type standard.float", why? Please piont out errors, Thanks.
:
: with Ada.Text_Io, Ada.Integer_Text_Io,Ada.Float_Text_Io;
: use Ada.Text_Io, Ada.Integer_Text_Io,Ada.Float_Text_Io;
:
: procedure sin is
:    sin_result : float;
:    i : integer;
:    function Calc_Sin (Num : Integer) return float is
:       begin
:          Sin_Result := Num - (Num ** 3) / 6 + (Num ** 5) / 120;

*** The problem appears to be in the line above. Sin_Result is of type
standard.float, so that appears to be OK. I wonder what the type is of the
stuff on the right hand side of the assignment statement?

:          return Sin_Result;
:    end Calc_Sin;
:
: begin
:
: put("input int: ");
: get(i);
: Sin_Result := Calc_Sin(Num=>i);
: Put(Item=>Sin_Result, Fore=>1, Aft=>4, Exp=>0);
:
: end sin;
:
:





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

* Re: Integer? float? confused...
  2001-03-20 21:31 Integer? float? confused WM
@ 2001-03-23  7:07 ` Sven Nilsson
  0 siblings, 0 replies; 9+ messages in thread
From: Sven Nilsson @ 2001-03-23  7:07 UTC (permalink / raw)


The problem is the line

 Sin_Result := Num - (Num ** 3) / 6 + (Num ** 5) / 120;

where you have a lot of integer arithmetic and expect a float in the
end. If you write it like this:

 Sin_Result := float(Num) - float(Num ** 3.0) / 6.0 + float(Num **
5)/120.0;

That should work. Now we've upcasted all the integers to floats and
perform only float arithmetic, which means that the end result is a
float as well.

-Sven



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

* Re: Integer? float? confused...
  2001-03-20 21:31 WM
                   ` (2 preceding siblings ...)
  2001-03-21  1:32 ` Ken Garlington
@ 2001-03-23 12:08 ` dis00109
  3 siblings, 0 replies; 9+ messages in thread
From: dis00109 @ 2001-03-23 12:08 UTC (permalink / raw)




WM wrote:

> I have written a program to calculate Sin. The compiler(AdaGIDE 6.23) said:
> "expected type standard.float", why? Please piont out errors, Thanks.
>
> with Ada.Text_Io, Ada.Integer_Text_Io,Ada.Float_Text_Io;
> use Ada.Text_Io, Ada.Integer_Text_Io,Ada.Float_Text_Io;
>
> procedure sin is
>    sin_result : float;
>    i : integer;
>    function Calc_Sin (Num : Integer) return float is
>       begin
>          Sin_Result := Num - (Num ** 3) / 6 + (Num ** 5) / 120;
>          return Sin_Result;
>    end Calc_Sin;
>
> begin
>
> put("input int: ");
> get(i);
> Sin_Result := Calc_Sin(Num=>i);
> Put(Item=>Sin_Result, Fore=>1, Aft=>4, Exp=>0);
>
> end sin;

Your problem is that you have correctly defined the result as a float
(Sin_Result:Float) however you need to convert the data from an integer to a
float as you perform the function on it. This mean that instead of writing:

Sin_Result := Num - (Num ** 3) / 6 + (Num ** 5) / 120;

you want to be writing:

Sin_Result:=Float(Num - (Num ** 3) / 6 + (Num ** 5) / 120);

This will solve your problem however don't forget to convert the integer to
radians before applying Sine to them (1 degree = pi / 180 therefore 90 degrees
= 1.57 radians).





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

* Re: Integer? float? confused...
  2001-03-20 21:31 WM
@ 2001-04-05 18:30 ` mark
  0 siblings, 0 replies; 9+ messages in thread
From: mark @ 2001-04-05 18:30 UTC (permalink / raw)


A reason for you experencing this error is that the computer is finding a 
integer and not a Float.

 A Float is a number with a decimal point in it e.g. 16.00 or 0.67 etc
 A integer is a number that is exact e.g. 1,2,3,4,5, 16 etc

 The fact that you have said that "Num" is an integer when it is then 
stated that it is Float make the machine confused. 
Try making "Num" a Float and that should clear up the error message!!


 Good luck


WM wrote:
> 
> 
> I have written a program to calculate Sin. The compiler(AdaGIDE 6.23) 
said:
> "expected type standard.float", why? Please piont out errors, Thanks.
> 
> with Ada.Text_Io, Ada.Integer_Text_Io,Ada.Float_Text_Io;
> use Ada.Text_Io, Ada.Integer_Text_Io,Ada.Float_Text_Io;
> 
> procedure sin is
>    sin_result : float;
>    i : integer;
>    function Calc_Sin (Num : Integer) return float is
>       begin
>          Sin_Result := Num - (Num ** 3) / 6 + (Num ** 5) / 120;
>          return Sin_Result;
>    end Calc_Sin;
> 
> REMOVED INCLUDED FILE sin;
> 
> 


--
Posted via CNET Help.com
http://www.help.com/



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

end of thread, other threads:[~2001-04-05 18:30 UTC | newest]

Thread overview: 9+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2001-03-20 21:31 Integer? float? confused WM
2001-03-23  7:07 ` Sven Nilsson
  -- strict thread matches above, loose matches on Subject: below --
2001-03-20 21:31 WM
2001-04-05 18:30 ` mark
2001-03-20 21:31 WM
2001-03-20 22:55 ` chris.danx
2001-03-20 22:59 ` EtienneB
2001-03-21  1:32 ` Ken Garlington
2001-03-23 12:08 ` dis00109

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