So either Math.ceil is not pristine and was replaced with some unexpected code or your call of Math.ceil is using the wrong characters. For example 1 instead of l or с instead of c etc. Try to retype it manually. Try to enter Math.ceil in the browser's console and inspect what it returns.
I have included the math.h header file but ceil() and floor() functions are not working. But pow() function works. I have given the error below.
/usr/bin/ld: /tmp/ccnYHlT4.o: in function `main':
stuff.c:(.text+0x1f): undefined reference to `floor'
collect2: error: ld returned 1 exit status
How to fix this?
Math.ceil() returns the ceiled value. It can't change the value of the variable it takes as argument, because Java passes arguments by value. So you need to do
hours = Math.ceil(hours);
The actual solution is to use double inside the ceil method.
Math.ceil(7 * 50 / 100) will return 3.0 even though the actual value resulting from 7*50/100 is 3.5. It is because since everything is int, the result of 350/100 itself will be 3.
If however, if you give Math.ceil(7 * 50 / 100D), the result would be 4.0.
So, the 4.999 in your question should be a double and not a result of an integer operation like 4999/1000.
Just make sure that whatever you give inside a the ceil is double and not an int.
This is probably because when you divide an Integer by an Integer you get an Integer back which has the same value as Decimal.round(RoundingMode.DOWN).
system.assertEquals(1, 8/5);
system.assertEquals(8/5, (8.0/5).round(RoundingMode.DOWN);
system.assertEquals(-1, -8/5);
system.assertEquals(-8/5, (-8.0/5).round(RoundingMode.DOWN);
system.assertEquals(2, 7/3);
system.assertEquals(7/3, (7.0/3).round(RoundingMode.DOWN);
If you know you have Integers and you want to get their ceiling, you could do something like:
public static Integer ceiling(Integer x, Integer y)
{
return Math.ceil(Decimal.valueOf(x).divide(y, /*digits*/ 1));
}
Integer Division
In Apex Code, similar to Java, when there are two like numeric data types (e.g. both Integers), then they are calculated in a way that returns the same data type. Integers cannot store fractions, so when you do something like 7/3, the fraction is silently discarded. As far as I know, integer division works in the same for every programming language in the world where the result of integer arithmetic is an integer.In many languages where the result of two integers being divided together results in an integer, the fraction is often dropped entirely, although exceptions do exist (some perform rounding, instead).
Parameter Promotion
You'll notice that there's no function Math.ceil that accepts an Integer. This means if you give it an integer, it will implicitly be cast to a floating point value before being processed by Math.ceil.
Arithmetic Promotion
When two numbers are operated on using the standard operators (+, -, /, *), if one of those parameters are a floating point value, the other one will also automatically become a floating point value. Similarly, if a integer and a long were involved in an operation, both numbers become long, and the return type becomes long. So, the goal is to create a situation where a floating point is returned. For example, this results in the correct result:
Integer x = Math.ceil(7.0/3).intValue();
Here, the 7.0 indicates a floating point operation. You'd also get the same effect if you did this:
Decimal x = 7;
Integer y = 3;
Integer z = Math.ceil(x/y).intValue();
This behavior is well-defined, and mimics the behavior in Java. You can read more about automatic widening conversion in the Java documentation, as well as rules about integer division. You'll find that, while not explicitly mentioned in the Apex Code Developer's Guide (as far as I can tell), it obeys the same rules.
you are casting an the result of integer division to a double.
You need to cast each part of the division to double BEFORE the result.
double pages = (double)total/(double)perPage;
The rest should work
(int)Math.ceil(3/2.0) will give answer 2
(int)Math.ceil(3/2) will give answer 1
In order to get the float value, you need to cast (or add .0) to one of the arguments
Hi everyone,
Today, I discovered a bug on LeetCode. It seems like the math.ceil function is not working properly. For example, math.ceil(3/6) should return 1.0, but it returns 0.0. If anyone from LeetCode is reading this, please look into fixing it. I was solving the Koko Eating Bananas problem and noticed that my old solution didn’t work, which was strange. It took me an hour to realize the issue was with the value returned by math.ceil on LeetCode.
https://ibb.co/JFBsQdK
https://ibb.co/BTj2nZG
The variable paintRequiredCeiling is only available in your calculate function. It doesn't exist in you printFunction. Similarly with other variables. You'll need to move them outside of the functions, or pass them, to get this to work.
There's no return statement in your calculate() function: you're calculating all these values, then throwing them away when your function ends, because those variables are all local to the function.
Similarly, your printFunction() function doesn't accept any values to print. So it expects the variables to be global, and since they're not, you get the error you got.
Now you could use global variables, but that is typically the wrong solution. Instead, learn how to use the return statement to return the results of your calculate() function, store those in variables in main(), and then pass them to printFunction().