0
0
PHPprogramming~5 mins

Float type and precision in PHP - Cheat Sheet & Quick Revision

Choose your learning style9 modes available
Recall & Review
beginner
What is a float in PHP?
A float is a number with a decimal point or in exponential form. It represents real numbers, like 3.14 or 2.5e3.
Click to reveal answer
intermediate
Why can floats have precision issues in PHP?
Floats are stored in binary, which can't exactly represent some decimal fractions. This causes small rounding errors.
Click to reveal answer
beginner
How can you control float precision when displaying numbers in PHP?
Use functions like number_format() or printf() to set how many decimal places to show.
Click to reveal answer
intermediate
What function can help compare floats safely in PHP?
Use a small tolerance value and check if the absolute difference is less than that, e.g., abs($a - $b) < 0.00001.
Click to reveal answer
intermediate
What is the typical size of a float in PHP?
A float usually uses 64 bits (double precision) following the IEEE 754 standard.
Click to reveal answer
What type of number does a float represent in PHP?
ANumbers with decimals or exponential form
BOnly whole numbers
CText strings
DBoolean values
Why might 0.1 + 0.2 not exactly equal 0.3 in PHP?
ABecause addition is not supported for floats
BBecause PHP does not support decimals
CBecause 0.3 is not a valid float
DBecause floats can have small rounding errors due to binary storage
Which PHP function helps format a float to 2 decimal places?
Anumber_format()
Bstrlen()
Carray_push()
Dstrtoupper()
How should you compare two floats for equality in PHP?
AConvert them to strings and compare
BUse the == operator directly
CCheck if their difference is smaller than a tiny tolerance
DUse the === operator
What is the typical precision of a PHP float?
ASingle precision (32 bits)
BDouble precision (64 bits)
C8 bits
D128 bits
Explain why floats can cause precision problems in PHP and how to handle them.
Think about how computers store decimal numbers.
You got /3 concepts.
    Describe how to safely compare two float numbers in PHP.
    Consider tiny differences caused by float representation.
    You got /3 concepts.