Concept
How can conditionals and the change of base formula validate logarithm input in PHP?
ComputerScienceOne / Life & Taxes
"38. Conditionals formula: log b ( x ) = log a ( x ) log a ( b ) If we can compute some base a , then we can compute any base b . Fortunately we have such a solution. Recall that the standard library provides a function to compute the natural logarithm, log() ). This is one of the fundamentals of problems solving: if a solution already exists, use it. In this case, a solution exists for a different, but similar problem (computing the natural logarithm), but we can adapt the solution using the change of base formula. In particular, if we have variables b (base) and x , we can compute log b ( x ) using log(x) / log(b) We have a problem similar to the examples in the previous section. The user could enter invalid values such as b = − 10 or x = − 2 . 54 (logarithms are undefined for non-positive values in any base). We want to ensure that b > 1 and x > 0. With conditionals, we can now do this. Once we have read in the input from the user we can make a check for good input using an if statement. 1 if($x <= 0 || $b <= 1) { 2 printf(\"Error: bad input!\n\n\"); 3 exit(1); 4 } This code has something new: exit(1) . The exit() function immediately terminates the script regardless of the rest of the code that may remain. The argument passed to exit is an integer that represents an error code . The convention is that zero indicates “no error” while non-zero values indicate some error. This is a simple way of performing error handling : if the user provides bad input, we inform them and quit the program, forcing them to run it again and provide good input. By prematurely terminating the program we avoid any illegal operation that would give a bad result. Alternatively, we could have split the conditions into two statements and given a more descriptive error message. We use this design in the full program which can be found in Code Sample 38.2. The program also takes the input as command line arguments. Now that we have conditionals, we can check that the correct number of arguments was provided by the user and quit in the event that they don’t provide the correct number."
Related Ideas
- How can conditionals validate logarithm inputs and handle errors?ComputerScienceOne · Life & Taxes
- How can the logarithm of a number be computed in an arbitrary base?ComputerScienceOne · Computing a Logarithm
- How can conditionals validate input for a logarithm calculator?ComputerScienceOne · Quadratic Roots Revisited
- How can a Java program compute a logarithm in an arbitrary base?ComputerScienceOne · Computing a Logarithm
- How can conditionals verify the number of command line arguments?ComputerScienceOne · Life & Taxes
- How do if, if-else, and if-else-if statements work in PHP?ComputerScienceOne · If, If-Else, If-Else-If Statements
- How can PHP libraries and math functions be used?ComputerScienceOne · Comments
- How can PHP read and clean user input from standard input?ComputerScienceOne · Converting Units