How to print an array with floating point numbers?

Discussion in 'Ruby' started by thiel, Nov 4, 2008.

  1. thiel

    thiel Guest

    Hi,

    I tried to print an array with a floating point numbers e.g. x[0,0] =
    55.123 with the format: printf("%0.04f\n", x[0,0])

    But it failed and I got the message: `printf': can't convert Array into
    Float (TypeError)

    When I tried printf("%0.04f\n", 55.123) I got the expected aswer: 55.1123

    Can anyone tell me how I can print an array with floating point numbers?

    Thanks in advance,

    Thiel Chang
    thiel, Nov 4, 2008
    #1
    1. Advertising


  2. > -----Original Message-----
    > From: thiel [mailto:]=20
    > Sent: Tuesday, November 04, 2008 3:43 PM
    > To: ruby-talk ML
    > Subject: How to print an array with floating point numbers?
    >=20
    > Hi,
    >=20
    > I tried to print an array with a floating point numbers e.g. x[0,0] =

    =3D
    > 55.123 with the format: printf("%0.04f\n", x[0,0])
    >


    Did you mean:
    printf("%0.04f\n",x[0][0])=20

    --
    Shourya
    Sarcar, Shourya C (GE Healthcare), Nov 4, 2008
    #2
    1. Advertising

  3. On Tue, 04 Nov 2008 11:12:31 +0100, thiel <> wrote:

    > Can anyone tell me how I can print an array with floating point numbers?


    printf:

    [0.1, 2.3, 4.5, 6.7, 8.9].each { |x| printf("%0.04f\n",x)}
    0.1000
    2.3000
    4.5000
    6.7000
    8.9000

    puts and '%':

    [0.1, 2.3, 4.5, 6.7, 8.9].each { |x| puts "%0.04f" % x}
    0.1000
    2.3000
    4.5000
    6.7000
    8.9000

    For multidimensional arrays you need to decide in which order you iterate
    over the indices. There's more than one convention to do so. Typical order
    1: The inner loop iterates over the first, the next outermost loop over
    the second index, and so on.
    Typical order 2: The inner loop iterates over the last, the next
    outermost loop over the previous index, and so on.

    HTH,

    Josef 'Jupp' Schugt
    --
    Blog: http://penpen.goodaddress.eu/
    PGP key (id 6CC6574F): http://wwwkeys.de.pgp.net/
    Jabber - http://www.jabber.org/ - contact information on request
    Josef 'Jupp' Schugt, Nov 4, 2008
    #3
  4. thiel

    thiel Guest

    Re: [?? Probable Spam] Re: How to print an array with floatingpoint numbers?

    Hi Josef,

    Many thanks for your advice. Solving the problem with your suggestion
    was quite trivial (at last :) )

    I ran into problems because I used a wrong multi dimensional array
    definition from the book B. Preissig and changed it into :

    --------------------------------------
    def Test.mda(width,height)
    Array.new(width).map!{ Array.new(height) }
    end

    x = Test.mda(6,2)

    # Some test data

    x[0][0] = 55.123
    x[1][0] = 39.234
    x[2][0] = 41.897
    x[3][0] = 40.456
    x[4][0] = 47.234
    x[5][0] = 51.567


    x[0][1] = 1
    x[1][1] = 2
    x[2][1] = 3
    x[3][1] = 4
    x[4][1] = 5
    x[5][1] = 6

    # And the testprogram

    x = x.sort {|v1, v2 | v2 <=> v1}

    for m in 0 .. 5
    if x[m][0] >= x[2][0] then
    printf("Greater %3.3f\n", x[m][0])
    else
    printf("Smaller %3.3f \n", x[m][0] )
    end
    end
    puts "Smallest value = #{x[5][0].to_s}"

    ------------------------------------

    And it works fine.

    Best regards,

    Thiel

    Josef 'Jupp' Schugt schreef:
    > <div class="moz-text-flowed" style="font-family: -moz-fixed">On Tue,
    > 04 Nov 2008 11:12:31 +0100, thiel <> wrote:
    >
    >> Can anyone tell me how I can print an array with floating point numbers?

    >
    > printf:
    >
    > [0.1, 2.3, 4.5, 6.7, 8.9].each { |x| printf("%0.04f\n",x)}
    > 0.1000
    > 2.3000
    > 4.5000
    > 6.7000
    > 8.9000
    >
    > puts and '%':
    >
    > [0.1, 2.3, 4.5, 6.7, 8.9].each { |x| puts "%0.04f" % x}
    > 0.1000
    > 2.3000
    > 4.5000
    > 6.7000
    > 8.9000
    >
    > For multidimensional arrays you need to decide in which order you
    > iterate over the indices. There's more than one convention to do so.
    > Typical order 1: The inner loop iterates over the first, the next
    > outermost loop over the second index, and so on.
    > Typical order 2: The inner loop iterates over the last, the next
    > outermost loop over the previous index, and so on.
    >
    > HTH,
    >
    > Josef 'Jupp' Schugt
    thiel, Nov 4, 2008
    #4
  5. thiel

    thiel Guest

    Hi Shourya,

    Many thanks for your answer. I solved the problem. The reason was I used
    a wrong multi dimensional array definition.

    Best regards,

    Thiel


    Sarcar, Shourya C (GE Healthcare) schreef:
    >
    >> -----Original Message-----
    >> From: thiel [mailto:]
    >> Sent: Tuesday, November 04, 2008 3:43 PM
    >> To: ruby-talk ML
    >> Subject: How to print an array with floating point numbers?
    >>
    >> Hi,
    >>
    >> I tried to print an array with a floating point numbers e.g. x[0,0] > 55.123 with the format: printf("%0.04f\n", x[0,0])
    >>
    >>

    >
    > Did you mean:
    > printf("%0.04f\n",x[0][0])
    >
    > --
    > Shourya
    >
    >
    >
    >
    >
    >
    thiel, Nov 4, 2008
    #5
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. H aka N
    Replies:
    15
    Views:
    15,621
    Ben Jones
    Mar 2, 2006
  2. Motaz Saad
    Replies:
    7
    Views:
    6,461
  3. Lorenzo Di Gregorio
    Replies:
    3
    Views:
    4,051
    Dennis Lee Bieber
    Nov 4, 2009
  4. Saraswati lakki
    Replies:
    0
    Views:
    1,298
    Saraswati lakki
    Jan 6, 2012
  5. teeshift
    Replies:
    2
    Views:
    242
    Chris Pearl
    Dec 1, 2006
Loading...

Share This Page