Take the 2-minute tour ×
Mathematica Stack Exchange is a question and answer site for users of Mathematica. It's 100% free, no registration required.
Table[StringForm["p=`1`", i], {i, 0.05, 0.5, 0.05}]
(*  
  {p=0.05`, p=0.1`, p=0.15000000000000002`, p=0.2`, p=0.25`, p=0. 30000000000000004`, 
   p=0.35000000000000003`, p=0.4`, p=0.45`, p=0.5`}
*)

Why do the results show small deviations from the expected numbers {..., 0.15, ..., 0.30, ...}?

share|improve this question
1  
I (kind of) translated your question this time. But the site's language is English. –  belisarius Jun 29 '13 at 3:13
    
Thanks for translated it –  Matematicoaficionado Jun 29 '13 at 3:17
2  
I believe the fundamental reason is that with fixed precision, floating point arithmetic -- on a machine that has to translate into binary and then back into decimal -- you get machine representational error of 1/10 and then: 0.05 + 0.05 + 0.05 // FullForm gives 0.15000000000000002. –  murray Jun 29 '13 at 3:21
1  
Table[StringForm["p=1`", N@i], {i, 5/100, 5/10, 5/100}]? –  belisarius Jun 29 '13 at 3:35
1  
This tutorial, which is part of overview of numbers and their forms, might be helpful explaining the ` and the representation of numbers. This tutorial explains further output formats. –  Michael E2 Jun 29 '13 at 15:36
show 7 more comments

1 Answer

I think what you are looking for may be

Table["p=" <> ToString @ NumberForm[i, 2], {i, 0.05, 0.5, 0.05}]

{"p=0.05", "p=0.1", "p=0.15", "p=0.2", "p=0.25", "p=0.3", "p=0.35", "p=0.4", "p=0.45", "p=0.5"}

This is the simplest way I can think of that produces output with numbers formatted in the way you request.

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.