Open main menu
Home
Random
Recent changes
Special pages
Community portal
Preferences
About Wikipedia
Disclaimers
Incubator escapee wiki
Search
User menu
Talk
Dark mode
Contributions
Create account
Log in
Editing
Floor and ceiling functions
(section)
Warning:
You are not logged in. Your IP address will be publicly visible if you make any edits. If you
log in
or
create an account
, your edits will be attributed to your username, along with other benefits.
Anti-spam check. Do
not
fill this in!
==Computer implementations== [[File:Int function.svg|thumb|right|<code>int</code> function from floating-point conversion in [[C (programming language)|C]]]] In most programming languages, the simplest method to convert a floating point number to an integer does not do floor or ceiling, but truncation. The reason for this is historical, as the first machines used [[ones' complement]] and truncation was simpler to implement (floor is simpler in [[two's complement]]). [[FORTRAN]] was defined to require this behavior and thus almost all processors implement conversion this way. Some consider this to be an unfortunate historical design decision that has led to bugs handling negative offsets and graphics on the negative side of the origin.{{cn|date=November 2018}} An [[arithmetic shift|arithmetic right-shift]] of a signed integer <math>x</math> by <math>n</math> is the same as <math>\left\lfloor \tfrac{x}{2^n} \right\rfloor</math>. Division by a power of 2 is often written as a right-shift, not for optimization as might be assumed, but because the floor of negative results is required. Assuming such shifts are "premature optimization" and replacing them with division can break software.{{cn|date=March 2019}} Many programming languages (including [[C (programming language)|C]], [[C++]],<ref>{{cite web | title=C++ reference of <code>floor</code> function | url=http://en.cppreference.com/w/cpp/numeric/math/floor | access-date=5 December 2010}} </ref><ref>{{cite web | title=C++ reference of <code>ceil</code> function | url=http://en.cppreference.com/w/cpp/numeric/math/ceil | access-date=5 December 2010}} </ref> [[C Sharp (programming language)|C#]],<ref>{{Cite web|url=https://docs.microsoft.com/en-us/dotnet/api/system.math.floor|title=Math.Floor Method (System)|last=dotnet-bot|website=docs.microsoft.com|language=en-us|access-date=28 November 2019}}</ref><ref>{{Cite web|url=https://docs.microsoft.com/en-us/dotnet/api/system.math.ceiling|title=Math.Ceiling Method (System)|last=dotnet-bot|website=docs.microsoft.com|language=en-us|access-date=28 November 2019}}</ref> [[Java (programming language)|Java]],<ref>{{Cite web|url=https://docs.oracle.com/javase/9/docs/api/java/lang/Math.html#floor-double-|title=Math (Java SE 9 & JDK 9 )|website=docs.oracle.com|language=en|access-date=20 November 2018}}</ref><ref>{{Cite web|url=https://docs.oracle.com/javase/9/docs/api/java/lang/Math.html#ceil-double-|title=Math (Java SE 9 & JDK 9 )|website=docs.oracle.com|language=en|access-date=20 November 2018}}</ref> [[Julia (programming language)|Julia]],<ref>{{Cite web|url=https://docs.julialang.org/en/v1/base/math/|title=Math (Julia v1.10)|website=docs.julialang.org/en/v1/|language=en|access-date=4 September 2024}}</ref> [[PHP]],<ref>{{cite web | title=PHP manual for <code>ceil</code> function | url=http://php.net/manual/function.ceil.php | access-date=18 July 2013}} </ref><ref>{{cite web | title=PHP manual for <code>floor</code> function | url=http://php.net/manual/function.floor.php | access-date=18 July 2013}} </ref> [[R (programming language)|R]],<ref>{{Cite web | url=https://stat.ethz.ch/R-manual/R-devel/library/base/html/Round.html | title=R: Rounding of Numbers}}</ref> and [[Python (programming language)|Python]]<ref>{{cite web | title=Python manual for <code>math</code> module | url=https://docs.python.org/2/library/math.html | access-date=18 July 2013}} </ref>) provide standard functions for floor and ceiling, usually called <code>floor</code> and <code>ceil</code>, or less commonly <code>ceiling</code>.<ref>Sullivan, p. 86.</ref> The language [[APL (programming language)|APL]] uses <code>βx</code> for floor. The [[J (programming language)|J Programming Language]], a follow-on to APL that is designed to use standard keyboard symbols, uses <code><.</code> for floor and <code>>.</code> for ceiling.<ref>{{cite web|title=Vocabulary|work=J Language|url=http://www.jsoftware.com/help/dictionary/vocabul.htm|access-date=6 September 2011}}</ref> [[ALGOL]] uses<code>entier</code> for floor. In [[Microsoft Excel]] the function <code>INT</code> rounds down rather than toward zero,<ref>{{cite web|url = https://support.microsoft.com/en-us/office/int-function-a6c4af9e-356d-4369-ab6a-cb1fd9d343ef | title = INT function | access-date = 29 October 2021}}</ref> while <code>FLOOR</code> rounds toward zero, the opposite of what "int" and "floor" do in other languages. Since 2010 <code>FLOOR</code> has been changed to error if the number is negative.<ref>{{cite web|url = https://support.microsoft.com/en-us/office/floor-function-14bb497c-24f2-4e04-b327-b0b4de5a8886 | title = FLOOR function | access-date = 29 October 2021}}</ref> The [[OpenDocument]] file format, as used by [[OpenOffice.org]], [[Libreoffice]] and others, <code>INT</code><ref>{{cite web|url = https://wiki.openoffice.org/wiki/Documentation/How_Tos/Calc:_INT_function | title = Documentation/How Tos/Calc: INT function | access-date = 29 October 2021}}</ref> and <code>FLOOR</code> both do floor, and <code>FLOOR</code> has a third argument to reproduce Excel's earlier behavior.<ref>{{cite web|url = https://wiki.openoffice.org/wiki/Documentation/How_Tos/Calc:_FLOOR_function | title = Documentation/How Tos/Calc: FLOOR function | access-date = 29 October 2021}}</ref>
Edit summary
(Briefly describe your changes)
By publishing changes, you agree to the
Terms of Use
, and you irrevocably agree to release your contribution under the
CC BY-SA 4.0 License
and the
GFDL
. You agree that a hyperlink or URL is sufficient attribution under the Creative Commons license.
Cancel
Editing help
(opens in new window)