[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

dividing by two and rounding up

Tom Norian

12/17/2007 6:42:00 PM

Hey all...I am hoping for a tip

I have a table I want to lay out dynamcially in rails where if a game
has 6 periods the first 3 periods will be in the first column and the
4-6 in the second column. If someone picks an odd number of periods ,
say 5, I want the 1-3 in the first column and 4 and 5 in the second.

I have figured out how to iterate through IF I could get 5/2 to yield me
3.

How do I divide a number (integer) by 2 and get a result that is like:

5/2 I want 3
6/2 I want 3
7/2 I want 4

I'm thinking perhaps I could test for oddness then add one to the result
if odd, but that seems kinda a lot of lines for something so simple.

half_periods = 0
if num_periods%2 == 1
half_periods = num_periods/2 +1
else
half_periods = num_periods/2
end

Is there a better way?
--
Posted via http://www.ruby-....

15 Answers

Lionel Bouton

12/17/2007 6:52:00 PM

0

Tom Norian wrote:
> [...]I'm thinking perhaps I could test for oddness then add one to the result
> if odd, but that seems kinda a lot of lines for something so simple.
>
> half_periods = 0
> if num_periods%2 == 1
> half_periods = num_periods/2 +1
> else
> half_periods = num_periods/2
> end
>
> Is there a better way?
>

Something like this ?

((num_periods + 0.5)/2).round

Corey Jewett

12/17/2007 6:55:00 PM

0


On Dec 17, 2007, at 10:42 , Tom Norian wrote:

> Hey all...I am hoping for a tip
>
> I have a table I want to lay out dynamcially in rails where if a game
> has 6 periods the first 3 periods will be in the first column and the
> 4-6 in the second column. If someone picks an odd number of periods ,
> say 5, I want the 1-3 in the first column and 4 and 5 in the second.
>
> I have figured out how to iterate through IF I could get 5/2 to
> yield me
> 3.
>
> How do I divide a number (integer) by 2 and get a result that is like:
>
> 5/2 I want 3
> 6/2 I want 3
> 7/2 I want 4
>
> I'm thinking perhaps I could test for oddness then add one to the
> result
> if odd, but that seems kinda a lot of lines for something so simple.
>
> half_periods = 0
> if num_periods%2 == 1
> half_periods = num_periods/2 +1
> else
> half_periods = num_periods/2
> end

(5/2.0).ceil

Corey

Phrogz

12/17/2007 6:57:00 PM

0

On Dec 17, 11:42 am, Tom Norian <tomnor...@yahoo.com> wrote:
> Hey all...I am hoping for a tip
>
> I have a table I want to lay out dynamcially in rails where if a game
> has 6 periods the first 3 periods will be in the first column and the
> 4-6 in the second column. If someone picks an odd number of periods ,
> say 5, I want the 1-3 in the first column and 4 and 5 in the second.
>
> I have figured out how to iterate through IF I could get 5/2 to yield me
> 3.
>
> How do I divide a number (integer) by 2 and get a result that is like:
>
> 5/2 I want 3
> 6/2 I want 3
> 7/2 I want 4
>
> I'm thinking perhaps I could test for oddness then add one to the result
> if odd, but that seems kinda a lot of lines for something so simple.
>
> half_periods = 0
> if num_periods%2 == 1
> half_periods = num_periods/2 +1
> else
> half_periods = num_periods/2
> end

irb(main):012:0> 1.upto(5){ |i|
irb(main):013:1* val = (i/2.0).ceil
irb(main):014:1> puts "#{i}/2 -> #{val}"
irb(main):015:1> }
1/2 -> 1
2/2 -> 1
3/2 -> 2
4/2 -> 2
5/2 -> 3

Chris Shea

12/17/2007 7:00:00 PM

0

On Dec 17, 11:42 am, Tom Norian <tomnor...@yahoo.com> wrote:
> Hey all...I am hoping for a tip
>
> I have a table I want to lay out dynamcially in rails where if a game
> has 6 periods the first 3 periods will be in the first column and the
> 4-6 in the second column. If someone picks an odd number of periods ,
> say 5, I want the 1-3 in the first column and 4 and 5 in the second.
>
> I have figured out how to iterate through IF I could get 5/2 to yield me
> 3.
>
> How do I divide a number (integer) by 2 and get a result that is like:
>
> 5/2 I want 3
> 6/2 I want 3
> 7/2 I want 4
>
> I'm thinking perhaps I could test for oddness then add one to the result
> if odd, but that seems kinda a lot of lines for something so simple.
>
> half_periods = 0
> if num_periods%2 == 1
> half_periods = num_periods/2 +1
> else
> half_periods = num_periods/2
> end
>
> Is there a better way?
> --
> Posted viahttp://www.ruby-....

Here's another way

half_periods = periods / 2 + periods % 2

HTH,
Chris

Sebastian Hungerecker

12/17/2007 7:03:00 PM

0

Tom Norian wrote:
> How do I divide a number (integer) by 2 and get a result that is like:
>
> 5/2 I want 3
> =C2=A06/2 I want 3
> 7/2 I want 4

>> (5/2.0).ceil
=3D> 3
>> (6/2.0).ceil
=3D> 3
>> (7/2.0).ceil
=3D> 4


HTH,
Sebastian
=2D-=20
NP: Explosions in the Sky - Greet Death
Jabber: sepp2k@jabber.org
ICQ: 205544826

Jano Svitok

12/17/2007 7:17:00 PM

0

On Dec 17, 2007 7:51 PM, Lionel Bouton <lionel-subscription@bouton.name> wrote:
> Tom Norian wrote:
> > [...]I'm thinking perhaps I could test for oddness then add one to the result
> > if odd, but that seems kinda a lot of lines for something so simple.
> >
> > half_periods = 0
> > if num_periods%2 == 1
> > half_periods = num_periods/2 +1
> > else
> > half_periods = num_periods/2
> > end
> >
> > Is there a better way?
> >
>
> Something like this ?

(num_periods / 2.0).ceil

or (num_periods + 1)/2 if we want to stay with integers.

Chris Shea

12/17/2007 7:24:00 PM

0

On Dec 17, 11:42 am, Tom Norian <tomnor...@yahoo.com> wrote:
> Hey all...I am hoping for a tip
>
> I have a table I want to lay out dynamcially in rails where if a game
> has 6 periods the first 3 periods will be in the first column and the
> 4-6 in the second column. If someone picks an odd number of periods ,
> say 5, I want the 1-3 in the first column and 4 and 5 in the second.
>
> I have figured out how to iterate through IF I could get 5/2 to yield me
> 3.
>
> How do I divide a number (integer) by 2 and get a result that is like:
>
> 5/2 I want 3
> 6/2 I want 3
> 7/2 I want 4
>
> I'm thinking perhaps I could test for oddness then add one to the result
> if odd, but that seems kinda a lot of lines for something so simple.
>
> half_periods = 0
> if num_periods%2 == 1
> half_periods = num_periods/2 +1
> else
> half_periods = num_periods/2
> end
>
> Is there a better way?
> --
> Posted viahttp://www.ruby-....

Here's a few ways:

require "test/unit"

PERIODS = [1,2,3,4,5,6,7,8,9,10]
PER_HALF = [1,1,2,2,3,3,4,4,5,5]

class TestPeriods < Test::Unit::TestCase
def test_div_two_plus_mod_two
assert_equal(PER_HALF, PERIODS.map {|period| period / 2 + period %
2})
end

def test_plus_mod_two_div_two
assert_equal(PER_HALF, PERIODS.map {|period| (period + period %
2) / 2})
end

def test_plus_one_div_two
assert_equal(PER_HALF, PERIODS.map {|period| (period + 1) / 2})
end

def test_div_2_ceil
assert_equal(PER_HALF, PERIODS.map {|period| (period / 2.0).ceil})
end

def test_plus_1_div_two_floor
assert_equal(PER_HALF, PERIODS.map {|period| ((period + 1.0) /
2).floor})
end

def test_div_2_round
assert_equal(PER_HALF, PERIODS.map {|period| (period /
2.0).round})
end
end

MonkeeSage

12/17/2007 7:30:00 PM

0

On Dec 17, 12:42 pm, Tom Norian <tomnor...@yahoo.com> wrote:
> Hey all...I am hoping for a tip
>
> I have a table I want to lay out dynamcially in rails where if a game
> has 6 periods the first 3 periods will be in the first column and the
> 4-6 in the second column. If someone picks an odd number of periods ,
> say 5, I want the 1-3 in the first column and 4 and 5 in the second.
>
> I have figured out how to iterate through IF I could get 5/2 to yield me
> 3.
>
> How do I divide a number (integer) by 2 and get a result that is like:
>
> 5/2 I want 3
> 6/2 I want 3
> 7/2 I want 4
>
> I'm thinking perhaps I could test for oddness then add one to the result
> if odd, but that seems kinda a lot of lines for something so simple.
>
> half_periods = 0
> if num_periods%2 == 1
> half_periods = num_periods/2 +1
> else
> half_periods = num_periods/2
> end
>
> Is there a better way?
> --
> Posted viahttp://www.ruby-....

Use floats and #round.

def div(x, y)
(x.to_f/y).round
end

div(5,2) # => 3
div(6,2) # => 3
div(7,2) # => 4

Regards,
Jordan

Martin Lüdtke

12/17/2007 7:46:00 PM

0

Corey Jewett schrieb:
> On Dec 17, 2007, at 10:42 , Tom Norian wrote:
>
> > Hey all...I am hoping for a tip
> >
> > I have a table I want to lay out dynamcially in rails where if a game
> > has 6 periods the first 3 periods will be in the first column and the
> > 4-6 in the second column. If someone picks an odd number of periods ,
> > say 5, I want the 1-3 in the first column and 4 and 5 in the second.
> >
> > I have figured out how to iterate through IF I could get 5/2 to
> > yield me
> > 3.
> >
> > How do I divide a number (integer) by 2 and get a result that is like:
> >
> > 5/2 I want 3
> > 6/2 I want 3
> > 7/2 I want 4
> >
> > I'm thinking perhaps I could test for oddness then add one to the
> > result
> > if odd, but that seems kinda a lot of lines for something so simple.
> >
> > half_periods = 0
> > if num_periods%2 == 1
> > half_periods = num_periods/2 +1
> > else
> > half_periods = num_periods/2
> > end
>
> (5/2.0).ceil
>
> Corey

(5+1) / 2

Tom Norian

12/17/2007 7:52:00 PM

0

Thanks guys.
--
Posted via http://www.ruby-....