1

I saw here that julia has some %f style in printing the floats. I would like to know if this is possible to do it in setting the name of some output file. I mean that in my program I have something like:

...
for epsilon in epsilon_array
    ...
    printfile = open("outputfile_epsilon$(epsilon).dat", "w")
    ...
end
...

So for instance, if I have epsilon=0.010000000 (float64), I want that the name of the outputfile is just outputfile_epsilon0.1.dat

EDIT:

For example consider the following "filling" of my the array:

epsilon_array = zeros(Float64,100)
iijj = 0.0
for ii in 1:100
    epsilon_array[ii] = iijj
    iijj += 0.01
end

If I take a look at some outputfile, I will have:

outputfile_epsilon0.9500000000000006.dat

So the problem is that there's a nasty number at the end of the float, that forces julia to print the whole integer.

user2820579
  • 3,261
  • 7
  • 30
  • 45
  • 1
    Maybe this post would help you: http://stackoverflow.com/questions/37031133/how-do-you-format-a-string-when-interpolated-in-julia. – niczky12 Jun 01 '16 at 18:01

2 Answers2

4

The crux of the problem here seems to be that you are not accounting for roundoff error in your summation. Float64 can't represent 0.01 exactly, so computers instead store the closest approximation. Each time you use epsilon in a computation, you increase the resulting error. The range and linspace functions are aware of this, and are thus able to avoid the problem:

iijj = 0:0.01:1
for ii in 1:100
    epsilon_array[ii] = iijj[ii]
end

Alternatively, you might decide that you just always really want 2 digits printed, in which case you could use printf-style formatting for the filename:

open(@sprintf("outputfile_epsilon%.2f.dat", epsilon), "w")
user1712368
  • 400
  • 3
  • 7
3

You can use the rstrip() function which will remove instances of a given character from the right side of a string (see documentation here). E.g.

epsilon=0.010000000
eps_String = rstrip("$epsilon", '0')
println(eps_String)
# 0.01

edit: I'm not certain that any kind of formatting is actually necessary here though. If your numbers are indeed stored as floats, then trailing zeros shouldn't be stored with them. E.g. even without the rstrip() in the example above I get:

julia> epsilon_array = [0.010000000, 0.02000000]
2-element Array{Float64,1}:
 0.01
 0.02    

julia> for epsilon in epsilon_array
           println("outputfile_epsilon$(epsilon).dat")
       end
outputfile_epsilon0.01.dat
outputfile_epsilon0.02.dat
Michael Ohlrogge
  • 10,559
  • 5
  • 48
  • 76
  • I am unmarking the question because the bug that I have. I added some edit to put on comparison why I was asking for trimming zeroes. – user2820579 Jun 01 '16 at 20:56
  • 2
    @user2820579 In that case, what about just rounding the number `round( )`, (you can specify the number of decimal precision you need) and then applying the above technique? – Michael Ohlrogge Jun 01 '16 at 20:58
  • It's true, I just did instead 25/100 for instance and I am getting the required float. I think that would be qualified as a minor bug, perhaps.. – user2820579 Jun 01 '16 at 21:04