Precision Loss in Type Conversion
C++
Medium
8 views
Problem Description
Demonstrate data loss when converting from larger to smaller data types. This teaches careful type selection.
Logic: Convert double to float to int and show precision loss
Official Solution
void question7_precision_loss() {
double preciseValue = 123.456789;
float lessPrec = (float)preciseValue;
int noDecimal = (int)preciseValue;
cout << "Original (double): " << preciseValue << endl;
cout << "As float: " << lessPrec << endl;
cout << "As int: " << noDecimal << endl;
}
Solutions (0)
No solutions submitted yet. Be the first!
No comments yet. Start the discussion!