Dismiss Notice
Join Physics Forums Today!
The friendliest, high quality science and math community on the planet! Everyone who loves science is here!

Pascal: How to use floor function in Pascal?

  1. Jun 10, 2012 #1

    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.
  2. jcsd
  3. Jun 10, 2012 #2
    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.
  4. Jun 10, 2012 #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:

    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.
  5. Jun 10, 2012 #4
    I guess I found the answer by myself, the function trunc would do the trick.
  6. Jun 19, 2012 #5
    Standard Pascal doesn't include a floor function.
Share this great discussion with others via Reddit, Google+, Twitter, or Facebook