Certification

cancel
Showing results for 
Search instead for 
Did you mean: 

CLD 12 Solution

In the solution for the CLD 12 exersize in the success package, the program converts a double to a string, then back to a double.  I think they did this in order to limit the number to two significant digits, but wouldn't it be easier to simply display the number with only two digits of precision?  Is converting a number back and forth like that acceptable according to the style checklist?

Forbes Black
Lapsed CLAD, LV 5 - LV 2022 (Yeah, I'm that old...)
0 Kudos
Message 1 of 3
(3,518 Views)

I have not looked at that exercise for a long time so am not certain what was intended.

 

That type of conversion only approximately limits significant digits for fractional numbers. It will work for integers. The reason is that some fractions cannot be exactly represented in binary in a finite number of bits. Any fraction with a divisor factor not a power of 2 will be in that catagory. Some, like 1/3, have infinitely repeating representations in both decimal and binary while others like 1/10 are finite in decimal but infinite in binary.  So converting the result of 1/10 to "0.10" as a string representation of a double number and then back to double will still result in a value with more than two significant digits.  The display format only affects the front panel appearance and not the value on the wire.

 

Lynn

 

Significant.png

0 Kudos
Message 2 of 3
(3,499 Views)

DLB to String and back to DBL CAN change the value of the data.  Simply changing the display format of a DBL CONNOT change the data value (only how the data is displayed) 

 

Given that there is a distinct difference between the data value with each method, I can easilly devise use-cases where each would be desirable.  I'm guessing the exercise is there to help demonstrate the difference between data value and data display.

Spoiler
But, I have a long history of guessing correctly on thing like this! Go! Magic 8-Ball!)

"Should be" isn't "Is" -Jay
0 Kudos
Message 3 of 3
(3,194 Views)