Pascal: How to use floor function in Pascal?

In summary, the individual is asking if Turbo Pascal for Windows has a floor function in its library and if not, if it is possible to write one. They then explain their current progress and ask if there is a way to save a real number as an integer in their program. They eventually find the solution themselves by using the trunc function.
  • #1
Arian.D
101
0
Hi,

I need to use floor function to write a program in Pascal, does pascal have it as a procedure/function in its library?

My compiler is Turbo Pascal for windows version 1.5, so it might be outdated. Somewhere on the internet I read that if I write 'uses math;' then I could use math functions that are in pascal's library, I did that but when I write floor or ceil the answer I get is 'Uknown identifier'.

Can I write the floor function in Pascal on my own or that would be too hard?

What should I do?

P.S: I'm a math major and I don't know much about programming like a CS student, I just need to write a bunch of programs in Pascal for my numerical analysis class.
 
Technology news on Phys.org
  • #2
Arian.D said:
Hi,

I need to use floor function to write a program in Pascal, does pascal have it as a procedure/function in its library?

My compiler is Turbo Pascal for windows version 1.5, so it might be outdated. Somewhere on the internet I read that if I write 'uses math;' then I could use math functions that are in pascal's library, I did that but when I write floor or ceil the answer I get is 'Uknown identifier'.

Can I write the floor function in Pascal on my own or that would be too hard?

What should I do?

P.S: I'm a math major and I don't know much about programming like a CS student, I just need to write a bunch of programs in Pascal for my numerical analysis class.

I don't know whether TPW has floor in its math library, but you can write your own. Pascal has integer division (div) and the real functions int and frac, which should be sufficient (remembering to deal with negative numbers appropriately). Ditto for ceil.
 
  • #3
Thanks. I programmed the floor function, that's solved.
Now at some point of the programming, it seems that I have no choice but to assign the value of a real variable to an integer variable. the operator x mod y works only when both x and y are integers, but the answer that my function floor() returns is a real number, and that's causing troubles for me.

Let me tell you what I'm trying to do. I'm writing a program that takes a real number as input and then converts it to any given base (2-9).

This is what I've done so far:

Program base_change;
uses wincrt;

Type no = array[1..50] of integer;


Var base:integer;
dig:integer;
x:real;
Var numb1:no;
numb2:no;

function floor (y:real): real;
Var m:real;

Begin

if y < 0 then
m := int(y) - 1
else
m := int(y);

floor := m;
End;

Procedure intconv(base:integer ; x:real);
Var n:integer;
number:real;
Begin
number := floor(x);
n:=1;
Repeat
Numb1[n] := (number mod base);
number:= (number div base);
n := n+1;

Until (number = 0);

for i := n-1 downto 1 do
write(Numb1);

End;

Begin
Write('Choose the base: ');
readln(base);
Write('Enter a real number to be converted into base',base,':');
readln(x);
intconv(base, x);
End.


I guess my general idea is correct. When I'm given a real number, first I convert the integer part using the procedure intconv, then I convert the fraction part using another procedure that I'll right after it. But my main problem for now is: How could I save the value of x (a real type data) in number(an integer type data)?
Is there anyways to do that?
Thanks in advance.
 
  • #4
I guess I found the answer by myself, the function trunc would do the trick.
 
  • #5
Standard Pascal doesn't include a floor function.
 

What is the purpose of the floor function in Pascal?

The floor function in Pascal is used to round a real number down to the nearest integer.

How do I use the floor function in Pascal?

To use the floor function in Pascal, you must first declare a variable of type real. Then use the keyword "floor" followed by the variable name and parenthesis containing the real number you want to round.

Can I use the floor function with negative numbers in Pascal?

Yes, the floor function in Pascal can be used with both positive and negative numbers. The function will round down to the nearest integer, regardless of the sign of the number.

What happens if I use the floor function with an integer in Pascal?

If the input to the floor function is already an integer, the function will return the same integer without rounding.

Are there any limitations to the floor function in Pascal?

The floor function in Pascal can only be used with real numbers, not with other data types such as strings or booleans. Additionally, the function will return an error if the input is not a number.

Similar threads

  • Programming and Computer Science
Replies
5
Views
1K
  • Programming and Computer Science
Replies
2
Views
338
  • Programming and Computer Science
Replies
11
Views
1K
  • Programming and Computer Science
Replies
2
Views
1K
  • Programming and Computer Science
2
Replies
37
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
6
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
7
Views
2K
  • Programming and Computer Science
Replies
10
Views
2K
Replies
6
Views
1K
  • Programming and Computer Science
Replies
11
Views
2K
Back
Top