Dividing by two and rounding up

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?

Tom N. wrote:

Is there a better way?

Something like this ?

((num_periods + 0.5)/2).round

On Dec 17, 2007, at 10:42 , Tom N. wrote:

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

Thanks guys.

On Dec 17, 11:42 am, Tom N. [email protected] wrote:

How do I divide a number (integer) by 2 and get a result that is like:
half_periods = num_periods/2 +1
else
half_periods = num_periods/2
end

Is there a better way?

Posted viahttp://www.ruby-forum.com/.

Here’s another way

half_periods = periods / 2 + periods % 2

HTH,
Chris

On Dec 17, 11:42 am, Tom N. [email protected] wrote:

How do I divide a number (integer) by 2 and get a result that is like:
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

Tom N. wrote:

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

(5/2.0).ceil
=> 3

(6/2.0).ceil
=> 3

(7/2.0).ceil
=> 4

HTH,
Sebastian

This is Ruby so fast is not what you are going for, but I can’t help
pointing out that (n+1)>>1 many be the ‘fastest’ option.

On Dec 17, 2007 7:51 PM, Lionel B. [email protected]
wrote:

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.

On Dec 17, 12:42 pm, Tom N. [email protected] wrote:

How do I divide a number (integer) by 2 and get a result that is like:
half_periods = num_periods/2 +1
else
half_periods = num_periods/2
end

Is there a better way?

Posted viahttp://www.ruby-forum.com/.

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

On Dec 18, 2007 12:12 AM, Tom N. [email protected] wrote:

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

(n+1)/2

irb(main):001:0> (1…10).map {|i| [i, (i+1)/2]}
=> [[1, 1], [2, 1], [3, 2], [4, 2], [5, 3], [6, 3], [7, 4], [8, 4],
[9, 5], [10, 5]]

martin

On Dec 17, 11:42 am, Tom N. removed_[email protected] wrote:

How do I divide a number (integer) by 2 and get a result that is like:
half_periods = num_periods/2 +1
else
half_periods = num_periods/2
end

Is there a better way?

Posted viahttp://www.ruby-forum.com/.

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

Tom N. wrote:

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

Here’s another way, one integer division, but 2 instructions:

a = 5.divmod(2)
result = a[1] + a[2]

Best regards,

Jari W.

Corey J. schrieb:

yield me
if odd, but that seems kinda a lot of lines for something so simple.
Corey
(5+1) / 2

Why not get the arrays:

irb(main):022:0> a
=> [1, 2, 3, 4, 5, 6]
irb(main):023:0> a.slice(0,3)
=> [1, 2, 3]
irb(main):024:0> a.slice(3,3)
=> [4, 5, 6]
irb(main):025:0> b
=> [1, 2, 3, 4, 5]
irb(main):026:0> b.slice(0,3)
=> [1, 2, 3]
irb(main):027:0> b.slice(3,3)
=> [4, 5]

then loop over them creating the columns.

-corey

On Dec 17, 2007 4:12 PM, Jari W.

Or, better yet

require ‘enumerator’

irb(main):036:0> a.each_slice(3) do |slice|
irb(main):037:1* puts slice.join(", “)
irb(main):038:1> end
1, 2, 3
4, 5, 6
=> nil
irb(main):039:0> b.each_slice(3) do |slice|
irb(main):040:1* puts slice.join(”, ")
irb(main):041:1> end
1, 2, 3
4, 5

This way, you don’t have to worry about it, since you are always
grabbing the first three.

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs