Pascal: How to use floor function in Pascal?

  • Thread starter Thread starter Arian.D
  • Start date Start date
  • Tags Tags
    Function Pascal
Click For Summary

Discussion Overview

The discussion revolves around the implementation and usage of the floor function in Pascal, particularly in the context of Turbo Pascal for Windows version 1.5. Participants explore whether the floor function is available in the standard library and discuss alternatives for handling real numbers in programming tasks related to numerical analysis.

Discussion Character

  • Technical explanation
  • Exploratory
  • Homework-related

Main Points Raised

  • One participant inquires about the availability of the floor function in Turbo Pascal's library and expresses difficulty in using it, receiving an 'Unknown identifier' error.
  • Another participant suggests that while the standard library may not include a floor function, it is possible to implement one using integer division and real functions like int and frac, with attention to negative numbers.
  • A participant shares their implementation of a custom floor function and describes their programming task of converting a real number to a different base, highlighting challenges with type conversion between real and integer variables.
  • One participant identifies that the trunc function could be used to address the issue of converting a real number to an integer.
  • A later reply states that standard Pascal does not include a floor function, indicating a potential limitation in the language's capabilities.

Areas of Agreement / Disagreement

Participants express differing views on the availability of the floor function in Turbo Pascal. While some suggest it can be implemented manually, others note that standard Pascal does not include it, leading to an unresolved discussion on the best approach to handle such functions.

Contextual Notes

Participants mention limitations related to the specific version of Turbo Pascal being used and the potential absence of certain mathematical functions in its library. There is also a focus on the need to manage type conversions between real and integer data types.

Arian.D
Messages
101
Reaction score
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
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.
 
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.
 
I guess I found the answer by myself, the function trunc would do the trick.
 
Standard Pascal doesn't include a floor function.
 

Similar threads

  • · Replies 2 ·
Replies
2
Views
3K
  • · Replies 6 ·
Replies
6
Views
3K
  • · Replies 11 ·
Replies
11
Views
2K
  • · Replies 7 ·
Replies
7
Views
2K
  • · Replies 20 ·
Replies
20
Views
4K
  • · Replies 2 ·
Replies
2
Views
2K
  • · Replies 10 ·
Replies
10
Views
2K
Replies
3
Views
2K
Replies
5
Views
2K
  • · Replies 9 ·
Replies
9
Views
2K