Questions tagged [floor]

A function common to many programming languages that returns the next lowest integer value by rounding the value down if necessary.

Returns the next lowest integer value by rounding down value if necessary. php.net

Examples

echo floor(1.3);    // returns   1
echo floor(10.9);   // returns  10
echo floor(-3.14);  // returns  -4
365 questions
110
votes
6 answers

Why does Math.Floor(Double) return a value of type Double?

I need to get the left hand side integer value from a decimal or double. For Ex: I need to get the value 4 from 4.6. I tried using Math.Floor function but it's returning a double value, for ex: It's returning 4.0 from 4.6. The MSDN documentation…
Sridhar
82
votes
5 answers

Why do lots of (old) programs use floor(0.5 + input) instead of round(input)?

The differences reside in the returned value giving inputs around tie-breaking I believe, such as this code: int main() { std::cout.precision(100); double input = std::nextafter(0.05, 0.0) / 0.1; double x1 = floor(0.5 + input); …
markzzz
  • 44,504
  • 107
  • 265
  • 458
64
votes
4 answers

Floor or ceiling of a pandas series in python?

I have a pandas series series. If I want to get the element-wise floor or ceiling, is there a built in method or do I have to write the function and use apply? I ask because the data is big so I appreciate efficiency. Also this question has not…
wolfsatthedoor
  • 5,669
  • 14
  • 38
  • 76
57
votes
17 answers

Round minute down to nearest quarter hour

I need to round times down to the nearest quarter hour in PHP. The times are being pulled from a MySQL database from a datetime column and formatted like 2010-03-18 10:50:00. Example: 10:50 needs to be 10:45 1:12 needs to be 1:00 3:28 needs to be…
Rob
  • 591
  • 1
  • 5
  • 5
46
votes
5 answers

Does casting to an int after std::floor guarantee the right result?

I'd like a floor function with the syntax int floor(double x); but std::floor returns a double. Is static_cast (std::floor(x)); guaranteed to give me the correct integer, or could I have an off-by-one problem? It seems to work, but I'd like…
Jesse Beder
  • 30,017
  • 18
  • 97
  • 140
45
votes
6 answers

Taking the floor of a float

I have found two ways of taking floors in Python: 3.1415 // 1 and import math math.floor(3.1415) The problem with the first approach is that it return a float (namely 3.0). The second approach feels clumsy and too long. Are there alternative…
Randomblue
  • 98,379
  • 133
  • 328
  • 526
42
votes
4 answers

Rounding to nearest fraction (half, quarter, etc.)

So, I need to create the following functions but my head can't think of any possibility in PHP without complicated math. Round always up to the nearest decimal (1.81 = 1.90, 1.89 = 1.90, 1.85 = 1.90) Round always down to the nearest decimal (1.81 =…
user1649331
34
votes
5 answers

Efficient integer floor function in C++

I want to define an efficient integer floor function, i.e. a conversion from float or double that performs truncation towards minus infinity. We can assume that the values are such that no integer overflow occurs. So far I have a few…
Yves Daoust
  • 48,767
  • 8
  • 39
  • 84
34
votes
1 answer

How do I create a new Joda DateTime truncated to the last hour?

I am pulling timestamps from a file that I want to create a new DateTime for, but I want to create the DateTime at the floor of the hour (or any Joda Period will do). How Can I do this?
WolfmanDragon
  • 7,505
  • 14
  • 47
  • 58
28
votes
1 answer

Ruby .ceil and .floor

I'm new to Ruby and I'm trying to figure out how ceil and floor works as I get different answers when a fraction or a decimal number is used (similar value). Below is what I have tried: puts 8/3.ceil == 2 #=> true puts 8/3.floor == 2 #=>…
misokuan
  • 283
  • 1
  • 3
  • 6
26
votes
2 answers

Getting the floor value of a number in SQLite?

I have a SQLite database with a table containing the scores of some league players in a bowling center. I'm trying to get the average of the Score column for each player ID. The problem with this is I only need the whole part of the average, and it…
Adam Smith
  • 4,432
  • 8
  • 41
  • 64
21
votes
6 answers

How to ceil, floor and round bcmath numbers?

I need to mimic the exact functionality of the ceil(), floor() and round() functions on bcmath numbers, I've already found a very similar question but unfortunately the answer provided isn't good enough for me since it lacks support for negative…
Alix Axel
  • 141,486
  • 84
  • 375
  • 483
19
votes
5 answers

Does this mean that Java Math.floor is extremely slow?

I don't Java much. I am writing some optimized math code and I was shocked by my profiler results. My code collects values, interleaves the data and then chooses the values based on that. Java runs slower than my C++ and MATLAB implementations. I…
Mikhail
  • 7,123
  • 9
  • 56
  • 121
18
votes
1 answer

Why does flooring infinity not throw some error?

I found myself having a case where the equivalent of floor $ 1/0 was being executed. λ> 1/0 Infinity This is normal behavior as far as I understand but, when Infinity is floor'd or ceiling'd λ> floor $ 1/0 …
gxtaillon
  • 940
  • 1
  • 17
  • 29
17
votes
4 answers

C integer division and floor

In C, is there a difference between integer division a/b and floor(a/b) where both a and b are integers? More specifically what happens during both processes?
CodeKingPlusPlus
  • 12,865
  • 46
  • 123
  • 204
1
2 3
24 25