07-25-2016 05:22 PM
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?
07-26-2016 07:54 AM
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
09-26-2016 08:54 PM
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.