ILIAS  release_5-4 Revision v5.4.26-12-gabc799a52e6
Ceiling.php
Go to the documentation of this file.
1 <?php
2 
4 
7 
8 class Ceiling
9 {
26  public static function ceiling($number, $significance = null)
27  {
28  if ($significance === null) {
29  self::floorCheck1Arg();
30  }
31 
32  try {
33  $number = Helpers::validateNumericNullBool($number);
34  $significance = Helpers::validateNumericNullSubstitution($significance, ($number < 0) ? -1 : 1);
35  } catch (Exception $e) {
36  return $e->getMessage();
37  }
38 
39  return self::argumentsOk((float) $number, (float) $significance);
40  }
41 
56  public static function math($number, $significance = null, $mode = 0)
57  {
58  try {
59  $number = Helpers::validateNumericNullBool($number);
60  $significance = Helpers::validateNumericNullSubstitution($significance, ($number < 0) ? -1 : 1);
61  $mode = Helpers::validateNumericNullSubstitution($mode, null);
62  } catch (Exception $e) {
63  return $e->getMessage();
64  }
65 
66  if (empty($significance * $number)) {
67  return 0.0;
68  }
69  if (self::ceilingMathTest((float) $significance, (float) $number, (int) $mode)) {
70  return floor($number / $significance) * $significance;
71  }
72 
73  return ceil($number / $significance) * $significance;
74  }
75 
89  public static function precise($number, $significance = 1)
90  {
91  try {
92  $number = Helpers::validateNumericNullBool($number);
93  $significance = Helpers::validateNumericNullSubstitution($significance, null);
94  } catch (Exception $e) {
95  return $e->getMessage();
96  }
97 
98  if (!$significance) {
99  return 0.0;
100  }
101  $result = $number / abs($significance);
102 
103  return ceil($result) * $significance * (($significance < 0) ? -1 : 1);
104  }
105 
109  private static function ceilingMathTest(float $significance, float $number, int $mode): bool
110  {
111  return ((float) $significance < 0) || ((float) $number < 0 && !empty($mode));
112  }
113 
119  private static function argumentsOk(float $number, float $significance)
120  {
121  if (empty($number * $significance)) {
122  return 0.0;
123  }
124  if (Helpers::returnSign($number) == Helpers::returnSign($significance)) {
125  return ceil($number / $significance) * $significance;
126  }
127 
128  return Functions::NAN();
129  }
130 
131  private static function floorCheck1Arg(): void
132  {
133  $compatibility = Functions::getCompatibilityMode();
134  if ($compatibility === Functions::COMPATIBILITY_EXCEL) {
135  throw new Exception('Excel requires 2 arguments for CEILING');
136  }
137  }
138 }
static ceiling($number, $significance=null)
CEILING.
Definition: Ceiling.php:26
static validateNumericNullBool($number)
Many functions accept null/false/true argument treated as 0/0/1.
Definition: Helpers.php:27
static math($number, $significance=null, $mode=0)
CEILING.MATH.
Definition: Ceiling.php:56
$result
static ceilingMathTest(float $significance, float $number, int $mode)
Let CEILINGMATH complexity pass Scrutinizer.
Definition: Ceiling.php:109
static precise($number, $significance=1)
CEILING.PRECISE.
Definition: Ceiling.php:89
static validateNumericNullSubstitution($number, $substitute)
Validate numeric, but allow substitute for null.
Definition: Helpers.php:51
static argumentsOk(float $number, float $significance)
Avoid Scrutinizer problems concerning complexity.
Definition: Ceiling.php:119
static getCompatibilityMode()
Return the current Compatibility Mode.
Definition: Functions.php:93