HW 4
A Blast From the Past:
Significant Figures!
1) Write a wrapper function called str.pro that converts a numerical value to its string representation using string() and removes all extra whitespace using strcompress().
IDL>
print,str(1.23445)
1.23445
IDL>
print,str(1234.45)
1234.45
2) Write a function called trans_dec.pro that accepts as input a string representation of a floating point value of order unity (e.g. ‘1.2345’ or ‘7.8374’) and a scalar number of significant figures, Nfig, of the return value. The function should then use some clever string manipulation to round the number to the correct number of significant figures. For example:
IDL>
print,trans_dec('1.2456',3)
1.25
IDL>
print,trans_dec('1.2456',1)
1.
IDL>
print,trans_dec('8.28199',5)
8.2820
IDL>
num = trans_dec('8.28199',5)
IDL>
help,num
NUM
STRING =
'8.2820'
Where the output is a string representation of a decimal value.
HINTS:
· At some point you’ll have to convert the input string to a decimal value. To preserve precision, use double() rather than float() for the conversion.
· When you call round(), use the /L64 keyword to preserve precision.
· Here’s a little snippet of my pseudo code to help you along:
;Move
decimal point so that correct number of sig ;figs are to the left. For Nfig=4
and ;value=’1.2345’, newval = ‘1234.5’
;Convert
this new number to a decimal value.
;Round
it.
;Convert
it back to a string.
;Move the decimal point back to its original ;position.
3) Okay, here’s where it all comes together! Write a function called sigfig.pro that accepts a numerical value along with a number of significant figures, Nfig, and returns the value as a string with the correct number of significant figures. Here’s how you can do it:
The trick is to convert the input value to scientific notation
expval = str(value, format=’(e)’)
Locate the position of the ‘e’ and extract the unit value, the sign of the order of magnitude and the value of the order of magnitude. For example, for the number 1.234567e-07, the unit value is ‘1.234567’, the sign of the order of magnitude is ‘-‘ and the order of magnitude is 7.
Next, round the unit value to Nfig significant figures using trans_dec(). Then move the decimal back to the correct location and return the converted value as a string representation of a decimal value. Here are some example cases:
IDL>
print,str(1.23445)
1.23445
IDL>
print,str(1234.45)
1234.45
IDL>
print,sigfig(1234.567,1)
1000.
IDL>
print,sigfig(1234.567,2)
1200.
IDL>
print,sigfig(1234.567,7)
1234.567
IDL>
print,sigfig(1234.567,5)
1234.6
IDL>
print,sigfig(-1234.567,5)
-1234.6
IDL>
print,sigfig(-0.1234567,5)
-0.12346
IDL>
print,sigfig(-0.1234567,2)
-0.12
IDL>
print,sigfig(-0.001234567,3)
-0.00123
HINT: When I did this problem, I didn’t figure it out and get it right in one night. So I doubt that you’ll be able to pull this off the night before the due date. Start early and think about it often. Test things out at the command line and diagram things out on scrap paper or a white board.