# Create a class - ideas

**URL:** https://rubytalk.org/t/create-a-class-ideas/60746
**Category:** ruby-talk
**Created:** [6 November 2010 09:20 UTC](https://rubytalk.org/t/create-a-class-ideas/60746 "2010-11-06T09:20:20Z")
**Posts on this page:** 15
**Page:** 1

<div class="post-metadata">

### Author: ![flebber](https://avatars.discourse-cdn.com/v4/letter/f/f9ae1b/32.png) [@flebber](https://rubytalk.org/u/flebber)
#### Post date: [6 November 2010 09:20 UTC](https://rubytalk.org/t/create-a-class-ideas/60746/1 "2010-11-06T09:20:20Z")

</div>

Evening

I am trying to create a class. I am struggling to figure the best flow  
to get the maths side to work.

So say that

R is a float given by user input  
P is a Total amount(Pool)  
Per is a variable %  
X is a variable that is a percentage of P defined by a maximum  
allocation.

So main = (( R \* X)/P)\*100

What I want to test is the value of X needed to equal Per from X's  
maximum allocation down.

What i am thinking but cant get right

Say

R = 5  
P = 10  
Per = 190  
X = max 80% of P

For X in main = Per ( Closest whole number or half that equals closest  
to but greater than Per)  
main = (( 5 \* 8)/10)\*100

So in example intially main equalled 400%. And answer I would want to  
resolve it to is X = 4 which is 200% as 3.50 equals 180%.

Any ideas?

---

<div class="post-metadata">

### Author: ![Robert\_K1](https://yyz1.discourse-cdn.com/flex029/user_avatar/rubytalk.org/robert_k1/32/1830_2.png) [@Robert\_K1](https://rubytalk.org/u/Robert_K1)
#### Post date: [6 November 2010 15:25 UTC](https://rubytalk.org/t/create-a-class-ideas/60746/2 "2010-11-06T15:25:13Z")

</div>

> I am trying to create a class. I am struggling to figure the best flow  
> to get the maths side to work.
> 
> So say that
> 
> R is a float given by user input  
> P is a Total amount(Pool)  
> Per is a variable %  
> X is a variable that is a percentage of P defined by a maximum  
> allocation.
> 
> So main = (( R \* X)/P)\*100

First of all you should get your variables right. Variable "Per" does not show up in the formula and "main" is not mentioned in the list.

The meaning of the formula is totally unclear to me. From what you gave you are calculating the fraction (R/P) multiply it with 100 (so you actually get (R/P) percent and now you multiply with another percentage (X). So you have a percentage of a percentage.

> What I want to test is the value of X needed to equal Per from X's  
> maximum allocation down.

Can you write down a formula that contains all variables in your list and point at the fixed ones (constants), user inputs and variables you want to resolve?

> What i am thinking but cant get right
> 
> Say
> 
> R = 5  
> P = 10  
> Per = 190  
> X = max 80% of P
> 
> For X in main = Per ( Closest whole number or half that equals closest  
> to but greater than Per)  
> main = (( 5 \* 8)/10)\*100
> 
> So in example intially main equalled 400%. And answer I would want to  
> resolve it to is X = 4 which is 200% as 3.50 equals 180%.
> 
> Any ideas?

Sorry you lost me somewhere along the path. Also it's tea time right now...

Cheers

&nbsp;&nbsp;robert

> **···**
>
> On 06.11.2010 10:19, flebber wrote:
> 
> --  
> remember.guy do |as, often| as.you\_can - without end  
> [http://blog.rubybestpractices.com/](http://blog.rubybestpractices.com/)

---

<div class="post-metadata">

### Author: ![flebber](https://avatars.discourse-cdn.com/v4/letter/f/f9ae1b/32.png) [@flebber](https://rubytalk.org/u/flebber)
#### Post date: [6 November 2010 22:50 UTC](https://rubytalk.org/t/create-a-class-ideas/60746/3 "2010-11-06T22:50:14Z")

</div>

So I want to check by changing X when in forumla "main" that it is =\>  
than "per"

In simple terms I want to calculate units needed to reach a rate of  
return, X represents the variable units and per is the ROI(return of  
investment rate I would deaire to acheive), R is the ratio of return  
and P is a pool or base amount, I am using base 10 to start off with.

I am trying to test X for a value, the only constraint on X is that it  
cannot exceed 80% of the P or Pool amount.  
So if I set per = 190%

R = 5  
P = 10  
Per = 190  
X = max 80% of P

For X in main \>= per

main = (( 5 \* X)/10)\*100 \>= 190%

so for X = 80% of P or 8 base units

main = (( 5 \* 8)/10)\*100

which would test out as

main \>= per

400 =\> 190 -

So when X is 8 units the main section is greater than per but its not  
the closest whole unit to per.

So when X = 40% or 4 base units

main = (( 5 \* 4)/10)\*100  
main \>= per  
200 \>= 190

this is the largest unit in 0.5 increments that remains greater than  
Per of 190 so I would want X once tested to resolve to this.

I hope that made sense.

> **···**
>
> On Nov 7, 2:24 am, Robert Klemme \<shortcut...@googlemail.com\> wrote:
> 
> > On 06.11.2010 10:19, flebber wrote:
> > 
> > \> I am trying to create a class. I am struggling to figure the best flow  
> > \> to get the maths side to work.
> > 
> > \> So say that
> > 
> > \> R is a float given by user input  
> > \> P is a Total amount(Pool)  
> > \> Per is a variable %  
> > \> X is a variable that is a percentage of P defined by a maximum  
> > \> allocation.
> > 
> > \> So main = (( R \* X)/P)\*100
> > 
> > First of all you should get your variables right. Variable "Per" does  
> > not show up in the formula and "main" is not mentioned in the list.
> > 
> > The meaning of the formula is totally unclear to me. From what you gave  
> > you are calculating the fraction (R/P) multiply it with 100 (so you  
> > actually get (R/P) percent and now you multiply with another percentage  
> > (X). So you have a percentage of a percentage.
> > 
> > \> What I want to test is the value of X needed to equal Per from X's  
> > \> maximum allocation down.
> > 
> > Can you write down a formula that contains all variables in your list  
> > and point at the fixed ones (constants), user inputs and variables you  
> > want to resolve?
> > 
> > \> What i am thinking but cant get right
> > 
> > \> Say
> > 
> > \> R = 5  
> > \> P = 10  
> > \> Per = 190  
> > \> X = max 80% of P
> > 
> > \> For X in main = Per ( Closest whole number or half that equals closest  
> > \> to but greater than Per)  
> > \> main = (( 5 \* 8)/10)\*100
> > 
> > \> So in example intially main equalled 400%. And answer I would want to  
> > \> resolve it to is X = 4 which is 200% as 3.50 equals 180%.
> > 
> > \> Any ideas?
> > 
> > Sorry you lost me somewhere along the path. Also it's tea time right now...
> > 
> > Cheers
> > 
> > ```
> > robert
> > 
> > ```
> > 
> > --  
> > remember.guy do |as, often| as.you\_can - without endhttp://blog.rubybestpractices.com/

---

<div class="post-metadata">

### Author: ![flebber](https://avatars.discourse-cdn.com/v4/letter/f/f9ae1b/32.png) [@flebber](https://rubytalk.org/u/flebber)
#### Post date: [6 November 2010 23:00 UTC](https://rubytalk.org/t/create-a-class-ideas/60746/4 "2010-11-06T23:00:14Z")

</div>

So how do I best get X to run a loop in 0.5 increments until it  
reaches the closest value that makes the left side of an equation  
greater or equal to the right. But where it is the lowest value that  
is greater than or equal to the the right.

Main =\> Per - where main is the lowest value it can be greater than  
per.

Cheers

Sayth

> **···**
>
> On Nov 7, 9:45 am, flebber \<flebber.c...@gmail.com\> wrote:
> 
> > On Nov 7, 2:24 am, Robert Klemme \<shortcut...@googlemail.com\> wrote:
> > 
> > \> On 06.11.2010 10:19, flebber wrote:
> > 
> > \> \> I am trying to create a class. I am struggling to figure the best flow  
> > \> \> to get the maths side to work.
> > 
> > \> \> So say that
> > 
> > \> \> R is a float given by user input  
> > \> \> P is a Total amount(Pool)  
> > \> \> Per is a variable %  
> > \> \> X is a variable that is a percentage of P defined by a maximum  
> > \> \> allocation.
> > 
> > \> \> So main = (( R \* X)/P)\*100
> > 
> > \> First of all you should get your variables right. Variable "Per" does  
> > \> not show up in the formula and "main" is not mentioned in the list.
> > 
> > \> The meaning of the formula is totally unclear to me. From what you gave  
> > \> you are calculating the fraction (R/P) multiply it with 100 (so you  
> > \> actually get (R/P) percent and now you multiply with another percentage  
> > \> (X). So you have a percentage of a percentage.
> > 
> > \> \> What I want to test is the value of X needed to equal Per from X's  
> > \> \> maximum allocation down.
> > 
> > \> Can you write down a formula that contains all variables in your list  
> > \> and point at the fixed ones (constants), user inputs and variables you  
> > \> want to resolve?
> > 
> > \> \> What i am thinking but cant get right
> > 
> > \> \> Say
> > 
> > \> \> R = 5  
> > \> \> P = 10  
> > \> \> Per = 190  
> > \> \> X = max 80% of P
> > 
> > \> \> For X in main = Per ( Closest whole number or half that equals closest  
> > \> \> to but greater than Per)  
> > \> \> main = (( 5 \* 8)/10)\*100
> > 
> > \> \> So in example intially main equalled 400%. And answer I would want to  
> > \> \> resolve it to is X = 4 which is 200% as 3.50 equals 180%.
> > 
> > \> \> Any ideas?
> > 
> > \> Sorry you lost me somewhere along the path. Also it's tea time right now...
> > 
> > \> Cheers
> > 
> > \> robert
> > 
> > \> --  
> > \> remember.guy do |as, often| as.you\_can - without endhttp://blog.rubybestpractices.com/
> > 
> > So I want to check by changing X when in forumla "main" that it is =\>  
> > than "per"
> > 
> > In simple terms I want to calculate units needed to reach a rate of  
> > return, X represents the variable units and per is the ROI(return of  
> > investment rate I would deaire to acheive), R is the ratio of return  
> > and P is a pool or base amount, I am using base 10 to start off with.
> > 
> > I am trying to test X for a value, the only constraint on X is that it  
> > cannot exceed 80% of the P or Pool amount.  
> > So if I set per = 190%
> > 
> > R = 5  
> > P = 10  
> > Per = 190  
> > X = max 80% of P
> > 
> > For X in main \>= per
> > 
> > main = (( 5 \* X)/10)\*100 \>= 190%
> > 
> > so for X = 80% of P or 8 base units
> > 
> > main = (( 5 \* 8)/10)\*100
> > 
> > which would test out as
> > 
> > main \>= per
> > 
> > 400 =\> 190 -
> > 
> > So when X is 8 units the main section is greater than per but its not  
> > the closest whole unit to per.
> > 
> > So when X = 40% or 4 base units
> > 
> > main = (( 5 \* 4)/10)\*100  
> > main \>= per  
> > 200 \>= 190
> > 
> > this is the largest unit in 0.5 increments that remains greater than  
> > Per of 190 so I would want X once tested to resolve to this.
> > 
> > I hope that made sense.

---

<div class="post-metadata">

### Author: ![Mike\_IMAP](https://avatars.discourse-cdn.com/v4/letter/m/13edae/32.png) [@Mike\_IMAP](https://rubytalk.org/u/Mike_IMAP)
#### Post date: [6 November 2010 23:21 UTC](https://rubytalk.org/t/create-a-class-ideas/60746/5 "2010-11-06T23:21:10Z")

</div>

The wording is pretty vague at several points, but I'm trying to work through it to make sense of it...

You've really lost me when 80% turns into 8 (or 8 base units). What is a base unit? And why would it be equal to 10%?  
Are you looking to vary X from 80% down by 5% increments?

Mike Cargal

mike@cargal.net

> **[blog.mikecargal.com is almost here!](http://blog.mikecargal.com)**
>
> The owner of this domain has not yet uploaded their website.

> **···**
>
> On Nov 6, 2010, at 7:00 PM, flebber wrote:
> 
> > On Nov 7, 9:45 am, flebber \<flebber.c...@gmail.com\> wrote:
> > 
> > > On Nov 7, 2:24 am, Robert Klemme \<shortcut...@googlemail.com\> wrote:
> > > 
> > > > On 06.11.2010 10:19, flebber wrote:
> > > 
> > > > > I am trying to create a class. I am struggling to figure the best flow  
> > > > > to get the maths side to work.
> > > 
> > > > > So say that
> > > 
> > > > > R is a float given by user input  
> > > > > P is a Total amount(Pool)  
> > > > > Per is a variable %  
> > > > > X is a variable that is a percentage of P defined by a maximum  
> > > > > allocation.
> > > 
> > > > > So main = (( R \* X)/P)\*100
> > > 
> > > > First of all you should get your variables right. Variable "Per" does  
> > > > not show up in the formula and "main" is not mentioned in the list.
> > > 
> > > > The meaning of the formula is totally unclear to me. From what you gave  
> > > > you are calculating the fraction (R/P) multiply it with 100 (so you  
> > > > actually get (R/P) percent and now you multiply with another percentage  
> > > > (X). So you have a percentage of a percentage.
> > > 
> > > > > What I want to test is the value of X needed to equal Per from X's  
> > > > > maximum allocation down.
> > > 
> > > > Can you write down a formula that contains all variables in your list  
> > > > and point at the fixed ones (constants), user inputs and variables you  
> > > > want to resolve?
> > > 
> > > > > What i am thinking but cant get right
> > > 
> > > > > Say
> > > 
> > > > > R = 5  
> > > > > P = 10  
> > > > > Per = 190  
> > > > > X = max 80% of P
> > > 
> > > > > For X in main = Per ( Closest whole number or half that equals closest  
> > > > > to but greater than Per)  
> > > > > main = (( 5 \* 8)/10)\*100
> > > 
> > > > > So in example intially main equalled 400%. And answer I would want to  
> > > > > resolve it to is X = 4 which is 200% as 3.50 equals 180%.
> > > 
> > > > > Any ideas?
> > > 
> > > > Sorry you lost me somewhere along the path. Also it's tea time right now...
> > > 
> > > > Cheers
> > > 
> > > > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;robert
> > > 
> > > > --  
> > > > remember.guy do |as, often| as.you\_can - without endhttp://blog.rubybestpractices.com/
> > > 
> > > So I want to check by changing X when in forumla "main" that it is =\>  
> > > than "per"
> > > 
> > > In simple terms I want to calculate units needed to reach a rate of  
> > > return, X represents the variable units and per is the ROI(return of  
> > > investment rate I would deaire to acheive), R is the ratio of return  
> > > and P is a pool or base amount, I am using base 10 to start off with.
> > > 
> > > I am trying to test X for a value, the only constraint on X is that it  
> > > cannot exceed 80% of the P or Pool amount.  
> > > So if I set per = 190%
> > > 
> > > R = 5  
> > > P = 10  
> > > Per = 190  
> > > X = max 80% of P
> > > 
> > > For X in main \>= per
> > > 
> > > main = (( 5 \* X)/10)\*100 \>= 190%
> > > 
> > > so for X = 80% of P or 8 base units
> > > 
> > > main = (( 5 \* 8)/10)\*100
> > > 
> > > which would test out as
> > > 
> > > main \>= per
> > > 
> > > 400 =\> 190 -
> > > 
> > > So when X is 8 units the main section is greater than per but its not  
> > > the closest whole unit to per.
> > > 
> > > So when X = 40% or 4 base units
> > > 
> > > main = (( 5 \* 4)/10)\*100  
> > > main \>= per  
> > > 200 \>= 190
> > > 
> > > this is the largest unit in 0.5 increments that remains greater than  
> > > Per of 190 so I would want X once tested to resolve to this.
> > > 
> > > I hope that made sense.
> > 
> > So how do I best get X to run a loop in 0.5 increments until it  
> > reaches the closest value that makes the left side of an equation  
> > greater or equal to the right. But where it is the lowest value that  
> > is greater than or equal to the the right.
> > 
> > Main =\> Per - where main is the lowest value it can be greater than  
> > per.
> > 
> > Cheers
> > 
> > Sayth

---

<div class="post-metadata">

### Author: ![Mike\_IMAP](https://avatars.discourse-cdn.com/v4/letter/m/13edae/32.png) [@Mike\_IMAP](https://rubytalk.org/u/Mike_IMAP)
#### Post date: [7 November 2010 00:06 UTC](https://rubytalk.org/t/create-a-class-ideas/60746/6 "2010-11-07T00:06:29Z")

</div>

> > > > I am trying to create a class. I am struggling to figure the best flow  
> > > > to get the maths side to work.
> > 
> > > > So say that
> > 
> > > > R is a float given by user input  
> > > > P is a Total amount(Pool)  
> > > > Per is a variable %  
> > > > X is a variable that is a percentage of P defined by a maximum  
> > > > allocation.
> > 
> > > > So main = (( R \* X)/P)\*100
> > 
> > > First of all you should get your variables right. Variable "Per" does  
> > > not show up in the formula and "main" is not mentioned in the list.
> > 
> > > The meaning of the formula is totally unclear to me. From what you gave  
> > > you are calculating the fraction (R/P) multiply it with 100 (so you  
> > > actually get (R/P) percent and now you multiply with another percentage  
> > > (X). So you have a percentage of a percentage.
> > 
> > > > What I want to test is the value of X needed to equal Per from X's  
> > > > maximum allocation down.
> > 
> > > Can you write down a formula that contains all variables in your list  
> > > and point at the fixed ones (constants), user inputs and variables you  
> > > want to resolve?
> > 
> > > > What i am thinking but cant get right
> > 
> > > > Say
> > 
> > > > R = 5  
> > > > P = 10  
> > > > Per = 190  
> > > > X = max 80% of P
> > 
> > > > For X in main = Per ( Closest whole number or half that equals closest  
> > > > to but greater than Per)  
> > > > main = (( 5 \* 8)/10)\*100
> > 
> > > > So in example intially main equalled 400%. And answer I would want to  
> > > > resolve it to is X = 4 which is 200% as 3.50 equals 180%.
> > 
> > > > Any ideas?
> > 
> > > Sorry you lost me somewhere along the path. Also it's tea time right now...
> > 
> > > Cheers
> > 
> > > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;robert
> > 
> > > --  
> > > remember.guy do |as, often| as.you\_can - without endhttp://blog.rubybestpractices.com/
> > 
> > So I want to check by changing X when in forumla "main" that it is =\>  
> > than "per"
> > 
> > In simple terms I want to calculate units needed to reach a rate of  
> > return, X represents the variable units and per is the ROI(return of  
> > investment rate I would deaire to acheive), R is the ratio of return  
> > and P is a pool or base amount, I am using base 10 to start off with.
> > 
> > I am trying to test X for a value, the only constraint on X is that it  
> > cannot exceed 80% of the P or Pool amount.  
> > So if I set per = 190%
> > 
> > R = 5  
> > P = 10  
> > Per = 190  
> > X = max 80% of P
> > 
> > For X in main \>= per
> > 
> > main = (( 5 \* X)/10)\*100 \>= 190%
> > 
> > so for X = 80% of P or 8 base units
> > 
> > main = (( 5 \* 8)/10)\*100
> > 
> > which would test out as
> > 
> > main \>= per
> > 
> > 400 =\> 190 -
> > 
> > So when X is 8 units the main section is greater than per but its not  
> > the closest whole unit to per.
> > 
> > So when X = 40% or 4 base units
> > 
> > main = (( 5 \* 4)/10)\*100  
> > main \>= per  
> > 200 \>= 190
> > 
> > this is the largest unit in 0.5 increments that remains greater than  
> > Per of 190 so I would want X once tested to resolve to this.
> > 
> > I hope that made sense.
> 
> So how do I best get X to run a loop in 0.5 increments until it  
> reaches the closest value that makes the left side of an equation  
> greater or equal to the right. But where it is the lowest value that  
> is greater than or equal to the the right.
> 
> Main =\> Per - where main is the lowest value it can be greater than  
> per.
> 
> Cheers
> 
> Sayth

Something's not right with your formulas (I suspect)

> > > > main = (( R \* X)/P)\*100

and X = X\*P

so... main =  
R\*(X\*P)  
----------- \* 100  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;P

the P's cancel out...  
R\*X\*100

varying P will not change the results of your calculations...

here's what I believe that you're asking for...

> **···**
>
> On Nov 6, 2010, at 7:00 PM, flebber wrote:
> 
> > On Nov 7, 9:45 am, flebber \<flebber.c...@gmail.com\> wrote:
> > 
> > > On Nov 7, 2:24 am, Robert Klemme \<shortcut...@googlemail.com\> wrote:
> > > 
> > > > On 06.11.2010 10:19, flebber wrote:
> 
> ================================  
> r = 5.0  
> p = 10.0  
> per = 190.0  
> x = 0.8
> 
> # begin &nbsp;&nbsp;main = ((r\*(x\*p))/p)\*100 &nbsp;&nbsp;lastSuccess = x if main \>= per &nbsp;&nbsp;x -= 0.05 end while main \>= per puts "#{lastSuccess\*100.0}%"
> 
> note: you can change p al day long and always get the same answer
> 
> # there are probably more "rubified" ways to express this. I've tried to maintain the approach you've stated. However, I would simplify the equation first, and since the last X that succeeds as you decrement is the same thing as the first that succeeds as you're going up, I'd probably turn t into something like...
> 
> r = 5.0  
> p = 10.0  
> per = 190.0  
> max\_x = 0.8
> 
> # x = 0.05 const = r\*100 # simplified without X x += 0.05 while (x\*const \< per) && (x \<= max\_x) puts x \<= max\_x ? "#{lastSuccess\*100.0}%" : "no answer"
> 
> Mike Cargal
> 
> mike@cargal.net
> 
> > **[blog.mikecargal.com is almost here!](http://blog.mikecargal.com)**
> >
> > The owner of this domain has not yet uploaded their website.

---

<div class="post-metadata">

### Author: ![flebber](https://avatars.discourse-cdn.com/v4/letter/f/f9ae1b/32.png) [@flebber](https://rubytalk.org/u/flebber)
#### Post date: [7 November 2010 01:16 UTC](https://rubytalk.org/t/create-a-class-ideas/60746/7 "2010-11-07T01:16:06Z")

</div>

> \>\>\>\> I am trying to create a class. I am struggling to figure the best flow  
> \>\>\>\> to get the maths side to work.
> 
> \>\>\>\> So say that
> 
> \>\>\>\> R is a float given by user input  
> \>\>\>\> P is a Total amount(Pool)  
> \>\>\>\> Per is a variable %  
> \>\>\>\> X is a variable that is a percentage of P defined by a maximum  
> \>\>\>\> allocation.
> 
> \>\>\>\> So main = (( R \* X)/P)\*100
> 
> \>\>\> First of all you should get your variables right. Variable "Per" does  
> \>\>\> not show up in the formula and "main" is not mentioned in the list.
> 
> \>\>\> The meaning of the formula is totally unclear to me. From what you gave  
> \>\>\> you are calculating the fraction (R/P) multiply it with 100 (so you  
> \>\>\> actually get (R/P) percent and now you multiply with another percentage  
> \>\>\> (X). So you have a percentage of a percentage.
> 
> \>\>\>\> What I want to test is the value of X needed to equal Per from X's  
> \>\>\>\> maximum allocation down.
> 
> \>\>\> Can you write down a formula that contains all variables in your list  
> \>\>\> and point at the fixed ones (constants), user inputs and variables you  
> \>\>\> want to resolve?
> 
> \>\>\>\> What i am thinking but cant get right
> 
> \>\>\>\> Say
> 
> \>\>\>\> R = 5  
> \>\>\>\> P = 10  
> \>\>\>\> Per = 190  
> \>\>\>\> X = max 80% of P
> 
> \>\>\>\> For X in main = Per ( Closest whole number or half that equals closest  
> \>\>\>\> to but greater than Per)  
> \>\>\>\> main = (( 5 \* 8)/10)\*100
> 
> \>\>\>\> So in example intially main equalled 400%. And answer I would want to  
> \>\>\>\> resolve it to is X = 4 which is 200% as 3.50 equals 180%.
> 
> \>\>\>\> Any ideas?
> 
> \>\>\> Sorry you lost me somewhere along the path. Also it's tea time right now...
> 
> \>\>\> Cheers
> 
> \>\>\> robert
> 
> \>\>\> --  
> \>\>\> remember.guy do |as, often| as.you\_can - without endhttp://blog.rubybestpractices.com/
> 
> \>\> So I want to check by changing X when in forumla "main" that it is =\>  
> \>\> than "per"
> 
> \>\> In simple terms I want to calculate units needed to reach a rate of  
> \>\> return, X represents the variable units and per is the ROI(return of  
> \>\> investment rate I would deaire to acheive), R is the ratio of return  
> \>\> and P is a pool or base amount, I am using base 10 to start off with.
> 
> \>\> I am trying to test X for a value, the only constraint on X is that it  
> \>\> cannot exceed 80% of the P or Pool amount.  
> \>\> So if I set per = 190%
> 
> \>\> R = 5  
> \>\> P = 10  
> \>\> Per = 190  
> \>\> X = max 80% of P
> 
> \>\> For X in main \>= per
> 
> \>\> main = (( 5 \* X)/10)\*100 \>= 190%
> 
> \>\> so for X = 80% of P or 8 base units
> 
> \>\> main = (( 5 \* 8)/10)\*100
> 
> \>\> which would test out as
> 
> \>\> main \>= per
> 
> \>\> 400 =\> 190 -
> 
> \>\> So when X is 8 units the main section is greater than per but its not  
> \>\> the closest whole unit to per.
> 
> \>\> So when X = 40% or 4 base units
> 
> \>\> main = (( 5 \* 4)/10)\*100  
> \>\> main \>= per  
> \>\> 200 \>= 190
> 
> \>\> this is the largest unit in 0.5 increments that remains greater than  
> \>\> Per of 190 so I would want X once tested to resolve to this.
> 
> \>\> I hope that made sense.
> 
> \> So how do I best get X to run a loop in 0.5 increments until it  
> \> reaches the closest value that makes the left side of an equation  
> \> greater or equal to the right. But where it is the lowest value that  
> \> is greater than or equal to the the right.
> 
> \> Main =\> Per - where main is the lowest value it can be greater than  
> \> per.
> 
> \> Cheers
> 
> \> Sayth
> 
> Something's not right with your formulas (I suspect)
> 
> \>\>\>\> main = (( R \* X)/P)\*100
> 
> and X = X\*P
> 
> so... main =  
> R\*(X\*P)  
> ----------- \* 100  
> P
> 
> the P's cancel out...  
> R\*X\*100
> 
> varying P will not change the results of your calculations...
> 
> # here's what I believe that you're asking for...
> 
> r = 5.0  
> p = 10.0  
> per = 190.0  
> x = 0.8
> 
> # begin main = ((r\*(x\*p))/p)\*100 lastSuccess = x if main \>= per x -= 0.05 end while main \>= per puts "#{lastSuccess\*100.0}%"
> 
> note: you can change p al day long and always get the same answer
> 
> # there are probably more "rubified" ways to express this. I've tried to maintain the approach you've stated. However, I would simplify the equation first, and since the last X that succeeds as you decrement is the same thing as the first that succeeds as you're going up, I'd probably turn t into something like...
> 
> r = 5.0  
> p = 10.0  
> per = 190.0  
> max\_x = 0.8
> 
> # x = 0.05 const = r\*100 # simplified without X x += 0.05 while (x\*const \< per) && (x \<= max\_x) puts x \<= max\_x ? "#{lastSuccess\*100.0}%" : "no answer"
> 
> Mike Cargal
> 
> m...@cargal.nethttp://blog.mikecargal.com

Thanks for looking at this for me.

> You've really lost me when 80% turns into 8 (or 8 base units). What is a base unit? And why would it be equal to 10%?  
> Are you looking to vary X from 80% down by 5% increments?

This test is the first of 4 I plan to make into one program. For each  
their while be a maximum allocation so in this case 80% so may be 40%  
etc. The pool in future will vary but I am using base 10 while I write  
it(hoping it would be clearer for another person reading it). So that  
means that X has a max allocation of units if 40% was the maximum  
allocation and base 10 then X would be 4 units and I would want my  
loop to test X from 0 to 4 in 0.5 increments.

I am going to need more time to read your solution as I haven't got it  
first read. I had started to look at [http://www.rubyist.net/~slagell/ruby/iterators.html](http://www.rubyist.net/~slagell/ruby/iterators.html)

and a solution flow similar to

> def WHILE(cond)

&nbsp;&nbsp;&nbsp;&nbsp;\> return if not cond  
&nbsp;&nbsp;&nbsp;&nbsp;\> yield  
&nbsp;&nbsp;&nbsp;&nbsp;\> retry  
&nbsp;&nbsp;&nbsp;&nbsp;\> end  
&nbsp;&nbsp;&nbsp;nil

> i=0; WHILE(i\<3) { print i; i+=1 }

012 nil

> **···**
>
> On Nov 7, 11:06 am, Mike Cargal \<m...@cargal.net\> wrote:
> 
> > On Nov 6, 2010, at 7:00 PM, flebber wrote:  
> > \> On Nov 7, 9:45 am, flebber \<flebber.c...@gmail.com\> wrote:  
> > \>\> On Nov 7, 2:24 am, Robert Klemme \<shortcut...@googlemail.com\> wrote:  
> > \>\>\> On 06.11.2010 10:19, flebber wrote:

---

<div class="post-metadata">

### Author: ![flebber](https://avatars.discourse-cdn.com/v4/letter/f/f9ae1b/32.png) [@flebber](https://rubytalk.org/u/flebber)
#### Post date: [7 November 2010 04:20 UTC](https://rubytalk.org/t/create-a-class-ideas/60746/8 "2010-11-07T04:20:25Z")

</div>

Actually after re-reading your post I get it. All but one small bit.

In your formula how does the ? "#{lastSuccess\*100.0}%" bit work? what  
data does lastSuccess pull.

x = 0.05  
const = r\*100 # simplified without X  
x += 0.05 while (x\*const \< per) && (x \<= max\_x)  
puts x \<= max\_x ? "#{lastSuccess\*100.0}%" : "no answer"

> **···**
>
> On Nov 7, 12:13 pm, flebber \<flebber.c...@gmail.com\> wrote:
> 
> > On Nov 7, 11:06 am, Mike Cargal \<m...@cargal.net\> wrote:
> > 
> > \> On Nov 6, 2010, at 7:00 PM, flebber wrote:
> > 
> > \> \> On Nov 7, 9:45 am, flebber \<flebber.c...@gmail.com\> wrote:  
> > \> \>\> On Nov 7, 2:24 am, Robert Klemme \<shortcut...@googlemail.com\> wrote:
> > 
> > \> \>\>\> On 06.11.2010 10:19, flebber wrote:
> > 
> > \> \>\>\>\> I am trying to create a class. I am struggling to figure the best flow  
> > \> \>\>\>\> to get the maths side to work.
> > 
> > \> \>\>\>\> So say that
> > 
> > \> \>\>\>\> R is a float given by user input  
> > \> \>\>\>\> P is a Total amount(Pool)  
> > \> \>\>\>\> Per is a variable %  
> > \> \>\>\>\> X is a variable that is a percentage of P defined by a maximum  
> > \> \>\>\>\> allocation.
> > 
> > \> \>\>\>\> So main = (( R \* X)/P)\*100
> > 
> > \> \>\>\> First of all you should get your variables right. Variable "Per" does  
> > \> \>\>\> not show up in the formula and "main" is not mentioned in the list.
> > 
> > \> \>\>\> The meaning of the formula is totally unclear to me. From what you gave  
> > \> \>\>\> you are calculating the fraction (R/P) multiply it with 100 (so you  
> > \> \>\>\> actually get (R/P) percent and now you multiply with another percentage  
> > \> \>\>\> (X). So you have a percentage of a percentage.
> > 
> > \> \>\>\>\> What I want to test is the value of X needed to equal Per from X's  
> > \> \>\>\>\> maximum allocation down.
> > 
> > \> \>\>\> Can you write down a formula that contains all variables in your list  
> > \> \>\>\> and point at the fixed ones (constants), user inputs and variables you  
> > \> \>\>\> want to resolve?
> > 
> > \> \>\>\>\> What i am thinking but cant get right
> > 
> > \> \>\>\>\> Say
> > 
> > \> \>\>\>\> R = 5  
> > \> \>\>\>\> P = 10  
> > \> \>\>\>\> Per = 190  
> > \> \>\>\>\> X = max 80% of P
> > 
> > \> \>\>\>\> For X in main = Per ( Closest whole number or half that equals closest  
> > \> \>\>\>\> to but greater than Per)  
> > \> \>\>\>\> main = (( 5 \* 8)/10)\*100
> > 
> > \> \>\>\>\> So in example intially main equalled 400%. And answer I would want to  
> > \> \>\>\>\> resolve it to is X = 4 which is 200% as 3.50 equals 180%.
> > 
> > \> \>\>\>\> Any ideas?
> > 
> > \> \>\>\> Sorry you lost me somewhere along the path. Also it's tea time right now...
> > 
> > \> \>\>\> Cheers
> > 
> > \> \>\>\> robert
> > 
> > \> \>\>\> --  
> > \> \>\>\> remember.guy do |as, often| as.you\_can - without endhttp://blog.rubybestpractices.com/
> > 
> > \> \>\> So I want to check by changing X when in forumla "main" that it is =\>  
> > \> \>\> than "per"
> > 
> > \> \>\> In simple terms I want to calculate units needed to reach a rate of  
> > \> \>\> return, X represents the variable units and per is the ROI(return of  
> > \> \>\> investment rate I would deaire to acheive), R is the ratio of return  
> > \> \>\> and P is a pool or base amount, I am using base 10 to start off with.
> > 
> > \> \>\> I am trying to test X for a value, the only constraint on X is that it  
> > \> \>\> cannot exceed 80% of the P or Pool amount.  
> > \> \>\> So if I set per = 190%
> > 
> > \> \>\> R = 5  
> > \> \>\> P = 10  
> > \> \>\> Per = 190  
> > \> \>\> X = max 80% of P
> > 
> > \> \>\> For X in main \>= per
> > 
> > \> \>\> main = (( 5 \* X)/10)\*100 \>= 190%
> > 
> > \> \>\> so for X = 80% of P or 8 base units
> > 
> > \> \>\> main = (( 5 \* 8)/10)\*100
> > 
> > \> \>\> which would test out as
> > 
> > \> \>\> main \>= per
> > 
> > \> \>\> 400 =\> 190 -
> > 
> > \> \>\> So when X is 8 units the main section is greater than per but its not  
> > \> \>\> the closest whole unit to per.
> > 
> > \> \>\> So when X = 40% or 4 base units
> > 
> > \> \>\> main = (( 5 \* 4)/10)\*100  
> > \> \>\> main \>= per  
> > \> \>\> 200 \>= 190
> > 
> > \> \>\> this is the largest unit in 0.5 increments that remains greater than  
> > \> \>\> Per of 190 so I would want X once tested to resolve to this.
> > 
> > \> \>\> I hope that made sense.
> > 
> > \> \> So how do I best get X to run a loop in 0.5 increments until it  
> > \> \> reaches the closest value that makes the left side of an equation  
> > \> \> greater or equal to the right. But where it is the lowest value that  
> > \> \> is greater than or equal to the the right.
> > 
> > \> \> Main =\> Per - where main is the lowest value it can be greater than  
> > \> \> per.
> > 
> > \> \> Cheers
> > 
> > \> \> Sayth
> > 
> > \> Something's not right with your formulas (I suspect)
> > 
> > \> \>\>\>\> main = (( R \* X)/P)\*100
> > 
> > \> and X = X\*P
> > 
> > \> so... main =  
> > \> R\*(X\*P)  
> > \> ----------- \* 100  
> > \> P
> > 
> > \> the P's cancel out...  
> > \> R\*X\*100
> > 
> > \> varying P will not change the results of your calculations...
> > 
> > \> here's what I believe that you're asking for...  
> > \> ================================  
> > \> r = 5.0  
> > \> p = 10.0  
> > \> per = 190.0  
> > \> x = 0.8
> > 
> > \> begin  
> > \> main = ((r\*(x\*p))/p)\*100  
> > \> lastSuccess = x if main \>= per  
> > \> x -= 0.05  
> > \> end while main \>= per  
> > \> puts "#{lastSuccess\*100.0}%"  
> > \> ===============================  
> > \> note: you can change p al day long and always get the same answer
> > 
> > \> there are probably more "rubified" ways to express this.  
> > \> I've tried to maintain the approach you've stated. However, I would simplify the equation first, and since the last X that succeeds as you decrement is the same thing as the first that succeeds as you're going up, I'd probably turn t into something like...  
> > \> ===============================  
> > \> r = 5.0  
> > \> p = 10.0  
> > \> per = 190.0  
> > \> max\_x = 0.8
> > 
> > \> x = 0.05  
> > \> const = r\*100 # simplified without X  
> > \> x += 0.05 while (x\*const \< per) && (x \<= max\_x)  
> > \> puts x \<= max\_x ? "#{lastSuccess\*100.0}%" : "no answer"  
> > \> ===============================
> > 
> > \> Mike Cargal
> > 
> > \> m...@cargal.nethttp://blog.mikecargal.com
> > 
> > Thanks for looking at this for me.
> > 
> > \> You've really lost me when 80% turns into 8 (or 8 base units). What is a base unit? And why would it be equal to 10%?  
> > \> Are you looking to vary X from 80% down by 5% increments?
> > 
> > This test is the first of 4 I plan to make into one program. For each  
> > their while be a maximum allocation so in this case 80% so may be 40%  
> > etc. The pool in future will vary but I am using base 10 while I write  
> > it(hoping it would be clearer for another person reading it). So that  
> > means that X has a max allocation of units if 40% was the maximum  
> > allocation and base 10 then X would be 4 units and I would want my  
> > loop to test X from 0 to 4 in 0.5 increments.
> > 
> > I am going to need more time to read your solution as I haven't got it  
> > first read. I had started to look athttp://www.rubyist.net/~slagell/ruby/iterators.html
> > 
> > and a solution flow similar to
> > 
> > \> def WHILE(cond)  
> > \> return if not cond  
> > \> yield  
> > \> retry  
> > \> end  
> > nil  
> > \> i=0; WHILE(i\<3) { print i; i+=1 }  
> > 012 nil- Hide quoted text -
> > 
> > - Show quoted text -

---

<div class="post-metadata">

### Author: ![John\_Mair](https://avatars.discourse-cdn.com/v4/letter/j/ac91a4/32.png) [@John\_Mair](https://rubytalk.org/u/John_Mair)
#### Post date: [7 November 2010 04:41 UTC](https://rubytalk.org/t/create-a-class-ideas/60746/9 "2010-11-07T04:41:07Z")

</div>

> and a solution flow similar to
> 
> \> def WHILE(cond)  
> &nbsp;&nbsp;&nbsp;&nbsp;\> return if not cond  
> &nbsp;&nbsp;&nbsp;&nbsp;\> yield  
> &nbsp;&nbsp;&nbsp;&nbsp;\> retry  
> &nbsp;&nbsp;&nbsp;&nbsp;\> end  
> &nbsp;&nbsp;&nbsp;nil  
> \> i=0; WHILE(i\<3) { print i; i+=1 }

This WHILE loop will not work because the condition is only evaluated  
once. If you want it to work you need to turn the condition into a  
lambda, like so:

def WHILE(cond)  
&nbsp;&nbsp;loop do  
&nbsp;&nbsp;&nbsp;&nbsp;return if !cond.()  
&nbsp;&nbsp;&nbsp;&nbsp;yield  
&nbsp;&nbsp;end  
end

and use like this:

i = 0  
WHILE(-\>{i \< 3}) { puts i; i+=1 }

> **···**
>
> --  
> Posted via [http://www.ruby-forum.com/\](http://www.ruby-forum.com/%5C).

---

<div class="post-metadata">

### Author: ![Robert\_K1](https://yyz1.discourse-cdn.com/flex029/user_avatar/rubytalk.org/robert_k1/32/1830_2.png) [@Robert\_K1](https://rubytalk.org/u/Robert_K1)
#### Post date: [7 November 2010 11:50 UTC](https://rubytalk.org/t/create-a-class-ideas/60746/10 "2010-11-07T11:50:19Z")

</div>

Where's the point in doing this when there are "while" loops already built into the language?

I still think we first need to get the math correct before we can come up with solutions. Formulas I have seen in this thread look like they could be solved with some simple transformations and do not need any nested intervals or similar approximation algorithms. So far I find the problem description quite confusing.

Cheers

&nbsp;&nbsp;robert

> **···**
>
> On 07.11.2010 05:41, John Mair wrote:
> 
> > > and a solution flow similar to
> > > 
> > > \> def WHILE(cond)  
> > > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;\> return if not cond  
> > > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;\> yield  
> > > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;\> retry  
> > > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;\> end  
> > > &nbsp;&nbsp;&nbsp;&nbsp;nil  
> > > \> i=0; WHILE(i\<3) { print i; i+=1 }
> > 
> > This WHILE loop will not work because the condition is only evaluated  
> > once. If you want it to work you need to turn the condition into a  
> > lambda, like so:
> > 
> > def WHILE(cond)  
> > &nbsp;&nbsp;&nbsp;loop do  
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;return if !cond.()  
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;yield  
> > &nbsp;&nbsp;&nbsp;end  
> > end
> > 
> > and use like this:
> > 
> > i = 0  
> > WHILE(-\>{i\< 3}) { puts i; i+=1 }
> 
> --  
> remember.guy do |as, often| as.you\_can - without end  
> [http://blog.rubybestpractices.com/](http://blog.rubybestpractices.com/)

---

<div class="post-metadata">

### Author: ![Mike\_IMAP](https://avatars.discourse-cdn.com/v4/letter/m/13edae/32.png) [@Mike\_IMAP](https://rubytalk.org/u/Mike_IMAP)
#### Post date: [7 November 2010 16:33 UTC](https://rubytalk.org/t/create-a-class-ideas/60746/11 "2010-11-07T16:33:14Z")

</div>

my bad...

r = 5.0  
p = 10.0  
per = 190.0  
max\_x = 0.8

x = 0.05  
const = r\*100 # simplified without X  
x += 0.05 while (x\*const \< per) && (x \<= max\_x)  
puts x \<= max\_x ? "#{x\*100.0}%" : "no answer"

Mike Cargal

mike@cargal.net

> **[blog.mikecargal.com is almost here!](http://blog.mikecargal.com)**
>
> The owner of this domain has not yet uploaded their website.

> **···**
>
> On Nov 7, 2010, at 12:20 AM, flebber wrote:
> 
> > On Nov 7, 12:13 pm, flebber \<flebber.c...@gmail.com\> wrote:
> > 
> > > On Nov 7, 11:06 am, Mike Cargal \<m...@cargal.net\> wrote:
> > > 
> > > > On Nov 6, 2010, at 7:00 PM, flebber wrote:
> > > 
> > > > > On Nov 7, 9:45 am, flebber \<flebber.c...@gmail.com\> wrote:
> > > > > 
> > > > > > On Nov 7, 2:24 am, Robert Klemme \<shortcut...@googlemail.com\> wrote:
> > > 
> > > > > > > On 06.11.2010 10:19, flebber wrote:
> > > 
> > > > > > > > I am trying to create a class. I am struggling to figure the best flow  
> > > > > > > > to get the maths side to work.
> > > 
> > > > > > > > So say that
> > > 
> > > > > > > > R is a float given by user input  
> > > > > > > > P is a Total amount(Pool)  
> > > > > > > > Per is a variable %  
> > > > > > > > X is a variable that is a percentage of P defined by a maximum  
> > > > > > > > allocation.
> > > 
> > > > > > > > So main = (( R \* X)/P)\*100
> > > 
> > > > > > > First of all you should get your variables right. Variable "Per" does  
> > > > > > > not show up in the formula and "main" is not mentioned in the list.
> > > 
> > > > > > > The meaning of the formula is totally unclear to me. From what you gave  
> > > > > > > you are calculating the fraction (R/P) multiply it with 100 (so you  
> > > > > > > actually get (R/P) percent and now you multiply with another percentage  
> > > > > > > (X). So you have a percentage of a percentage.
> > > 
> > > > > > > > What I want to test is the value of X needed to equal Per from X's  
> > > > > > > > maximum allocation down.
> > > 
> > > > > > > Can you write down a formula that contains all variables in your list  
> > > > > > > and point at the fixed ones (constants), user inputs and variables you  
> > > > > > > want to resolve?
> > > 
> > > > > > > > What i am thinking but cant get right
> > > 
> > > > > > > > Say
> > > 
> > > > > > > > R = 5  
> > > > > > > > P = 10  
> > > > > > > > Per = 190  
> > > > > > > > X = max 80% of P
> > > 
> > > > > > > > For X in main = Per ( Closest whole number or half that equals closest  
> > > > > > > > to but greater than Per)  
> > > > > > > > main = (( 5 \* 8)/10)\*100
> > > 
> > > > > > > > So in example intially main equalled 400%. And answer I would want to  
> > > > > > > > resolve it to is X = 4 which is 200% as 3.50 equals 180%.
> > > 
> > > > > > > > Any ideas?
> > > 
> > > > > > > Sorry you lost me somewhere along the path. Also it's tea time right now...
> > > 
> > > > > > > Cheers
> > > 
> > > > > > > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;robert
> > > 
> > > > > > > --  
> > > > > > > remember.guy do |as, often| as.you\_can - without endhttp://blog.rubybestpractices.com/
> > > 
> > > > > > So I want to check by changing X when in forumla "main" that it is =\>  
> > > > > > than "per"
> > > 
> > > > > > In simple terms I want to calculate units needed to reach a rate of  
> > > > > > return, X represents the variable units and per is the ROI(return of  
> > > > > > investment rate I would deaire to acheive), R is the ratio of return  
> > > > > > and P is a pool or base amount, I am using base 10 to start off with.
> > > 
> > > > > > I am trying to test X for a value, the only constraint on X is that it  
> > > > > > cannot exceed 80% of the P or Pool amount.  
> > > > > > So if I set per = 190%
> > > 
> > > > > > R = 5  
> > > > > > P = 10  
> > > > > > Per = 190  
> > > > > > X = max 80% of P
> > > 
> > > > > > For X in main \>= per
> > > 
> > > > > > main = (( 5 \* X)/10)\*100 \>= 190%
> > > 
> > > > > > so for X = 80% of P or 8 base units
> > > 
> > > > > > main = (( 5 \* 8)/10)\*100
> > > 
> > > > > > which would test out as
> > > 
> > > > > > main \>= per
> > > 
> > > > > > 400 =\> 190 -
> > > 
> > > > > > So when X is 8 units the main section is greater than per but its not  
> > > > > > the closest whole unit to per.
> > > 
> > > > > > So when X = 40% or 4 base units
> > > 
> > > > > > main = (( 5 \* 4)/10)\*100  
> > > > > > main \>= per  
> > > > > > 200 \>= 190
> > > 
> > > > > > this is the largest unit in 0.5 increments that remains greater than  
> > > > > > Per of 190 so I would want X once tested to resolve to this.
> > > 
> > > > > > I hope that made sense.
> > > 
> > > > > So how do I best get X to run a loop in 0.5 increments until it  
> > > > > reaches the closest value that makes the left side of an equation  
> > > > > greater or equal to the right. But where it is the lowest value that  
> > > > > is greater than or equal to the the right.
> > > 
> > > > > Main =\> Per - where main is the lowest value it can be greater than  
> > > > > per.
> > > 
> > > > > Cheers
> > > 
> > > > > Sayth
> > > 
> > > > Something's not right with your formulas (I suspect)
> > > 
> > > > > > > > main = (( R \* X)/P)\*100
> > > 
> > > > and X = X\*P
> > > 
> > > > so... main =  
> > > > R\*(X\*P)  
> > > > ----------- \* 100  
> > > > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;P
> > > 
> > > > the P's cancel out...  
> > > > R\*X\*100
> > > 
> > > > varying P will not change the results of your calculations...
> > > 
> > > > # here's what I believe that you're asking for...
> > > > 
> > > > r = 5.0  
> > > > p = 10.0  
> > > > per = 190.0  
> > > > x = 0.8
> > > 
> > > > # begin &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;main = ((r\*(x\*p))/p)\*100 &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;lastSuccess = x if main \>= per &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;x -= 0.05 end while main \>= per puts "#{lastSuccess\*100.0}%"
> > > > 
> > > > note: you can change p al day long and always get the same answer
> > > 
> > > > # there are probably more "rubified" ways to express this. I've tried to maintain the approach you've stated. However, I would simplify the equation first, and since the last X that succeeds as you decrement is the same thing as the first that succeeds as you're going up, I'd probably turn t into something like...
> > > > 
> > > > r = 5.0  
> > > > p = 10.0  
> > > > per = 190.0  
> > > > max\_x = 0.8
> > > 
> > > > # x = 0.05 const = r\*100 # simplified without X x += 0.05 while (x\*const \< per) && (x \<= max\_x) puts x \<= max\_x ? "#{lastSuccess\*100.0}%" : "no answer"
> > > 
> > > > Mike Cargal
> > > 
> > > > m...@cargal.nethttp://blog.mikecargal.com
> > > 
> > > Thanks for looking at this for me.
> > > 
> > > > You've really lost me when 80% turns into 8 (or 8 base units). What is a base unit? And why would it be equal to 10%?  
> > > > Are you looking to vary X from 80% down by 5% increments?
> > > 
> > > This test is the first of 4 I plan to make into one program. For each  
> > > their while be a maximum allocation so in this case 80% so may be 40%  
> > > etc. The pool in future will vary but I am using base 10 while I write  
> > > it(hoping it would be clearer for another person reading it). So that  
> > > means that X has a max allocation of units if 40% was the maximum  
> > > allocation and base 10 then X would be 4 units and I would want my  
> > > loop to test X from 0 to 4 in 0.5 increments.
> > > 
> > > I am going to need more time to read your solution as I haven't got it  
> > > first read. I had started to look athttp://www.rubyist.net/~slagell/ruby/iterators.html
> > > 
> > > and a solution flow similar to
> > > 
> > > \> def WHILE(cond)  
> > > &nbsp;&nbsp;&nbsp;&nbsp;\> return if not cond  
> > > &nbsp;&nbsp;&nbsp;&nbsp;\> yield  
> > > &nbsp;&nbsp;&nbsp;&nbsp;\> retry  
> > > &nbsp;&nbsp;&nbsp;&nbsp;\> end  
> > > &nbsp;&nbsp;&nbsp;nil  
> > > \> i=0; WHILE(i\<3) { print i; i+=1 }  
> > > 012 nil- Hide quoted text -
> > > 
> > > - Show quoted text -
> > 
> > Actually after re-reading your post I get it. All but one small bit.
> > 
> > In your formula how does the ? "#{lastSuccess\*100.0}%" bit work? what  
> > data does lastSuccess pull.
> > 
> > x = 0.05  
> > const = r\*100 # simplified without X  
> > x += 0.05 while (x\*const \< per) && (x \<= max\_x)  
> > puts x \<= max\_x ? "#{lastSuccess\*100.0}%" : "no answer"

---

<div class="post-metadata">

### Author: ![John\_Mair](https://avatars.discourse-cdn.com/v4/letter/j/ac91a4/32.png) [@John\_Mair](https://rubytalk.org/u/John_Mair)
#### Post date: [7 November 2010 12:07 UTC](https://rubytalk.org/t/create-a-class-ideas/60746/12 "2010-11-07T12:07:47Z")

</div>

> Where's the point in doing this when there are "while" loops already  
> built into the language?  
> &nbsp;&nbsp;robert

fun  
🙂

> **···**
>
> --  
> Posted via [http://www.ruby-forum.com/\](http://www.ruby-forum.com/%5C).

---

<div class="post-metadata">

### Author: ![flebber](https://avatars.discourse-cdn.com/v4/letter/f/f9ae1b/32.png) [@flebber](https://rubytalk.org/u/flebber)
#### Post date: [8 November 2010 10:45 UTC](https://rubytalk.org/t/create-a-class-ideas/60746/13 "2010-11-08T10:45:20Z")

</div>

> \>\>\>\>\>\>\> I am trying to create a class. I am struggling to figure the best flow  
> \>\>\>\>\>\>\> to get the maths side to work.
> 
> \>\>\>\>\>\>\> So say that
> 
> \>\>\>\>\>\>\> R is a float given by user input  
> \>\>\>\>\>\>\> P is a Total amount(Pool)  
> \>\>\>\>\>\>\> Per is a variable %  
> \>\>\>\>\>\>\> X is a variable that is a percentage of P defined by a maximum  
> \>\>\>\>\>\>\> allocation.
> 
> \>\>\>\>\>\>\> So main = (( R \* X)/P)\*100
> 
> \>\>\>\>\>\> First of all you should get your variables right. Variable "Per" does  
> \>\>\>\>\>\> not show up in the formula and "main" is not mentioned in the list.
> 
> \>\>\>\>\>\> The meaning of the formula is totally unclear to me. From what you gave  
> \>\>\>\>\>\> you are calculating the fraction (R/P) multiply it with 100 (so you  
> \>\>\>\>\>\> actually get (R/P) percent and now you multiply with another percentage  
> \>\>\>\>\>\> (X). So you have a percentage of a percentage.
> 
> \>\>\>\>\>\>\> What I want to test is the value of X needed to equal Per from X's  
> \>\>\>\>\>\>\> maximum allocation down.
> 
> \>\>\>\>\>\> Can you write down a formula that contains all variables in your list  
> \>\>\>\>\>\> and point at the fixed ones (constants), user inputs and variables you  
> \>\>\>\>\>\> want to resolve?
> 
> \>\>\>\>\>\>\> What i am thinking but cant get right
> 
> \>\>\>\>\>\>\> Say
> 
> \>\>\>\>\>\>\> R = 5  
> \>\>\>\>\>\>\> P = 10  
> \>\>\>\>\>\>\> Per = 190  
> \>\>\>\>\>\>\> X = max 80% of P
> 
> \>\>\>\>\>\>\> For X in main = Per ( Closest whole number or half that equals closest  
> \>\>\>\>\>\>\> to but greater than Per)  
> \>\>\>\>\>\>\> main = (( 5 \* 8)/10)\*100
> 
> \>\>\>\>\>\>\> So in example intially main equalled 400%. And answer I would want to  
> \>\>\>\>\>\>\> resolve it to is X = 4 which is 200% as 3.50 equals 180%.
> 
> \>\>\>\>\>\>\> Any ideas?
> 
> \>\>\>\>\>\> Sorry you lost me somewhere along the path. Also it's tea time right now...
> 
> \>\>\>\>\>\> Cheers
> 
> \>\>\>\>\>\> robert
> 
> \>\>\>\>\>\> --  
> \>\>\>\>\>\> remember.guy do |as, often| as.you\_can - without endhttp://blog.rubybestpractices.com/
> 
> \>\>\>\>\> So I want to check by changing X when in forumla "main" that it is =\>  
> \>\>\>\>\> than "per"
> 
> \>\>\>\>\> In simple terms I want to calculate units needed to reach a rate of  
> \>\>\>\>\> return, X represents the variable units and per is the ROI(return of  
> \>\>\>\>\> investment rate I would deaire to acheive), R is the ratio of return  
> \>\>\>\>\> and P is a pool or base amount, I am using base 10 to start off with.
> 
> \>\>\>\>\> I am trying to test X for a value, the only constraint on X is that it  
> \>\>\>\>\> cannot exceed 80% of the P or Pool amount.  
> \>\>\>\>\> So if I set per = 190%
> 
> \>\>\>\>\> R = 5  
> \>\>\>\>\> P = 10  
> \>\>\>\>\> Per = 190  
> \>\>\>\>\> X = max 80% of P
> 
> \>\>\>\>\> For X in main \>= per
> 
> \>\>\>\>\> main = (( 5 \* X)/10)\*100 \>= 190%
> 
> \>\>\>\>\> so for X = 80% of P or 8 base units
> 
> \>\>\>\>\> main = (( 5 \* 8)/10)\*100
> 
> \>\>\>\>\> which would test out as
> 
> \>\>\>\>\> main \>= per
> 
> \>\>\>\>\> 400 =\> 190 -
> 
> \>\>\>\>\> So when X is 8 units the main section is greater than per but its not  
> \>\>\>\>\> the closest whole unit to per.
> 
> \>\>\>\>\> So when X = 40% or 4 base units
> 
> \>\>\>\>\> main = (( 5 \* 4)/10)\*100  
> \>\>\>\>\> main \>= per  
> \>\>\>\>\> 200 \>= 190
> 
> \>\>\>\>\> this is the largest unit in 0.5 increments that remains greater than  
> \>\>\>\>\> Per of 190 so I would want X once tested to resolve to this.
> 
> \>\>\>\>\> I hope that made sense.
> 
> \>\>\>\> So how do I best get X to run a loop in 0.5 increments until it  
> \>\>\>\> reaches the closest value that makes the left side of an equation  
> \>\>\>\> greater or equal to the right. But where it is the lowest value that  
> \>\>\>\> is greater than or equal to the the right.
> 
> \>\>\>\> Main =\> Per - where main is the lowest value it can be greater than  
> \>\>\>\> per.
> 
> \>\>\>\> Cheers
> 
> \>\>\>\> Sayth
> 
> \>\>\> Something's not right with your formulas (I suspect)
> 
> \>\>\>\>\>\>\> main = (( R \* X)/P)\*100
> 
> \>\>\> and X = X\*P
> 
> \>\>\> so... main =  
> \>\>\> R\*(X\*P)  
> \>\>\> ----------- \* 100  
> \>\>\> P
> 
> \>\>\> the P's cancel out...  
> \>\>\> R\*X\*100
> 
> \>\>\> varying P will not change the results of your calculations...
> 
> \>\>\> here's what I believe that you're asking for...  
> \>\>\> ================================  
> \>\>\> r = 5.0  
> \>\>\> p = 10.0  
> \>\>\> per = 190.0  
> \>\>\> x = 0.8
> 
> \>\>\> begin  
> \>\>\> main = ((r\*(x\*p))/p)\*100  
> \>\>\> lastSuccess = x if main \>= per  
> \>\>\> x -= 0.05  
> \>\>\> end while main \>= per  
> \>\>\> puts "#{lastSuccess\*100.0}%"  
> \>\>\> ===============================  
> \>\>\> note: you can change p al day long and always get the same answer
> 
> \>\>\> there are probably more "rubified" ways to express this.  
> \>\>\> I've tried to maintain the approach you've stated. However, I would simplify the equation first, and since the last X that succeeds as you decrement is the same thing as the first that succeeds as you're going up, I'd probably turn t into something like...  
> \>\>\> ===============================  
> \>\>\> r = 5.0  
> \>\>\> p = 10.0  
> \>\>\> per = 190.0  
> \>\>\> max\_x = 0.8
> 
> \>\>\> x = 0.05  
> \>\>\> const = r\*100 # simplified without X  
> \>\>\> x += 0.05 while (x\*const \< per) && (x \<= max\_x)  
> \>\>\> puts x \<= max\_x ? "#{lastSuccess\*100.0}%" : "no answer"  
> \>\>\> ===============================
> 
> \>\>\> Mike Cargal
> 
> \>\>\> m...@cargal.nethttp://blog.mikecargal.com
> 
> \>\> Thanks for looking at this for me.
> 
> \>\>\> You've really lost me when 80% turns into 8 (or 8 base units). What is a base unit? And why would it be equal to 10%?  
> \>\>\> Are you looking to vary X from 80% down by 5% increments?
> 
> \>\> This test is the first of 4 I plan to make into one program. For each  
> \>\> their while be a maximum allocation so in this case 80% so may be 40%  
> \>\> etc. The pool in future will vary but I am using base 10 while I write  
> \>\> it(hoping it would be clearer for another person reading it). So that  
> \>\> means that X has a max allocation of units if 40% was the maximum  
> \>\> allocation and base 10 then X would be 4 units and I would want my  
> \>\> loop to test X from 0 to 4 in 0.5 increments.
> 
> \>\> I am going to need more time to read your solution as I haven't got it  
> \>\> first read. I had started to look athttp://www.rubyist.net/~slagell/ruby/iterators.html
> 
> \>\> and a solution flow similar to
> 
> \>\> \> def WHILE(cond)  
> \>\> \> return if not cond  
> \>\> \> yield  
> \>\> \> retry  
> \>\> \> end  
> \>\> nil  
> \>\> \> i=0; WHILE(i\<3) { print i; i+=1 }  
> \>\> 012 nil- Hide quoted text -
> 
> \>\> - Show quoted text -
> 
> \> Actually after re-reading your post I get it. All but one small bit.
> 
> \> In your formula how does the ? "#{lastSuccess\*100.0}%" bit work? what  
> \> data does lastSuccess pull.
> 
> \> x = 0.05  
> \> const = r\*100 # simplified without X  
> \> x += 0.05 while (x\*const \< per) && (x \<= max\_x)  
> \> puts x \<= max\_x ? "#{lastSuccess\*100.0}%" : "no answer"
> 
> my bad...
> 
> r = 5.0  
> p = 10.0  
> per = 190.0  
> max\_x = 0.8
> 
> x = 0.05  
> const = r\*100 # simplified without X  
> x += 0.05 while (x\*const \< per) && (x \<= max\_x)  
> puts x \<= max\_x ? "#{x\*100.0}%" : "no answer"
> 
> Mike Cargal
> 
> m...@cargal.nethttp://blog.mikecargal.com

> I still think we first need to get the math correct before we can come  
> up with solutions. Formulas I have seen in this thread look like they  
> could be solved with some simple transformations and do not need any  
> nested intervals or similar approximation algorithms. So far I find the  
> problem description quite confusing.

Essentially without formulas all I am calculating is how many units at  
a specified rate of return it would take to reach a percentage of rate  
of return. Only additionally I have specified a maximum unit  
allocation. There would ultimately be several options with different  
return rates and max allocations from each pool. A pool is a  
percentage subset of a bank.

> x = 0.05  
> const = r\*100 # simplified without X  
> x += 0.05 while (x\*const \< per) && (x \<= max\_x)  
> puts x \<= max\_x ? "#{x\*100.0}%" : "no answer"

Mikes solution definitely works for working units(X)..thank you. I  
need to set constants and obtain user inputs for different scenarios  
and call it to the function.

> could be solved with some simple transformations and do not need any

I need to lookup what a transformation is...

> **···**
>
> On Nov 8, 3:33 am, Mike Cargal \<m...@cargal.net\> wrote:
> 
> > On Nov 7, 2010, at 12:20 AM, flebber wrote:  
> > \> On Nov 7, 12:13 pm, flebber \<flebber.c...@gmail.com\> wrote:  
> > \>\> On Nov 7, 11:06 am, Mike Cargal \<m...@cargal.net\> wrote:  
> > \>\>\> On Nov 6, 2010, at 7:00 PM, flebber wrote:  
> > \>\>\>\> On Nov 7, 9:45 am, flebber \<flebber.c...@gmail.com\> wrote:  
> > \>\>\>\>\> On Nov 7, 2:24 am, Robert Klemme \<shortcut...@googlemail.com\> wrote:  
> > \>\>\>\>\>\> On 06.11.2010 10:19, flebber wrote:

---

<div class="post-metadata">

### Author: ![flebber](https://avatars.discourse-cdn.com/v4/letter/f/f9ae1b/32.png) [@flebber](https://rubytalk.org/u/flebber)
#### Post date: [10 November 2010 01:45 UTC](https://rubytalk.org/t/create-a-class-ideas/60746/14 "2010-11-10T01:45:15Z")

</div>

This is where I was headed with it....any suggestions apprecaited.

Bank = $500.00 # later to be a running total calculated

def ROIcalc  
const = r\*100 # simplified without X  
x += 0.05 while (x\*const \< per) && (x \<= max\_x)  
puts x \<= max\_x ? "#{x\*100.0}%" : "no answer"  
end

def pool  
if Bank \> 200 then Bank \* 0.05  
Else 10.00  
end

# Scenario 1  
a = ROIcalc(per = 190, max\_x = 0.8, puts "What ratio of return do you  
expect?" r = gets.chomp, pool)  
b = ROIcalc(per = 200, max\_x = 0.4, r = 12.00, pool)  
# output result if valid  
# or return no valid result choose another scenario or end.

This is where I was headed.

Bank = $500.00 # later to be a running total calculated

def ROIcalc  
const = r\*100 # simplified without X  
x += 0.05 while (x\*const \< per) && (x \<= max\_x)  
puts x \<= max\_x ? "#{x\*100.0}%" : "no answer"  
end

def pool  
if Bank \> 200 then Bank \* 0.05  
Else $10.00  
end

# Scenario 1  
a = ROIcalc(per = 190, max\_x = 0.8, puts "What ratio of return do you  
expect?" r = gets.chomp, pool)  
b = ROIcalc(per = 200, max\_x = 0.4, r = 12.00, pool)  
# output result if valid  
# or return no valid result choose another scenario or end.

> **···**
>
> On Nov 8, 9:40 pm, flebber \<flebber.c...@gmail.com\> wrote:
> 
> > On Nov 8, 3:33 am, Mike Cargal \<m...@cargal.net\> wrote:
> > 
> > \> On Nov 7, 2010, at 12:20 AM, flebber wrote:
> > 
> > \> \> On Nov 7, 12:13 pm, flebber \<flebber.c...@gmail.com\> wrote:  
> > \> \>\> On Nov 7, 11:06 am, Mike Cargal \<m...@cargal.net\> wrote:
> > 
> > \> \>\>\> On Nov 6, 2010, at 7:00 PM, flebber wrote:
> > 
> > \> \>\>\>\> On Nov 7, 9:45 am, flebber \<flebber.c...@gmail.com\> wrote:  
> > \> \>\>\>\>\> On Nov 7, 2:24 am, Robert Klemme \<shortcut...@googlemail.com\> wrote:
> > 
> > \> \>\>\>\>\>\> On 06.11.2010 10:19, flebber wrote:
> > 
> > \> \>\>\>\>\>\>\> I am trying to create a class. I am struggling to figure the best flow  
> > \> \>\>\>\>\>\>\> to get the maths side to work.
> > 
> > \> \>\>\>\>\>\>\> So say that
> > 
> > \> \>\>\>\>\>\>\> R is a float given by user input  
> > \> \>\>\>\>\>\>\> P is a Total amount(Pool)  
> > \> \>\>\>\>\>\>\> Per is a variable %  
> > \> \>\>\>\>\>\>\> X is a variable that is a percentage of P defined by a maximum  
> > \> \>\>\>\>\>\>\> allocation.
> > 
> > \> \>\>\>\>\>\>\> So main = (( R \* X)/P)\*100
> > 
> > \> \>\>\>\>\>\> First of all you should get your variables right. Variable "Per" does  
> > \> \>\>\>\>\>\> not show up in the formula and "main" is not mentioned in the list.
> > 
> > \> \>\>\>\>\>\> The meaning of the formula is totally unclear to me. From what you gave  
> > \> \>\>\>\>\>\> you are calculating the fraction (R/P) multiply it with 100 (so you  
> > \> \>\>\>\>\>\> actually get (R/P) percent and now you multiply with another percentage  
> > \> \>\>\>\>\>\> (X). So you have a percentage of a percentage.
> > 
> > \> \>\>\>\>\>\>\> What I want to test is the value of X needed to equal Per from X's  
> > \> \>\>\>\>\>\>\> maximum allocation down.
> > 
> > \> \>\>\>\>\>\> Can you write down a formula that contains all variables in your list  
> > \> \>\>\>\>\>\> and point at the fixed ones (constants), user inputs and variables you  
> > \> \>\>\>\>\>\> want to resolve?
> > 
> > \> \>\>\>\>\>\>\> What i am thinking but cant get right
> > 
> > \> \>\>\>\>\>\>\> Say
> > 
> > \> \>\>\>\>\>\>\> R = 5  
> > \> \>\>\>\>\>\>\> P = 10  
> > \> \>\>\>\>\>\>\> Per = 190  
> > \> \>\>\>\>\>\>\> X = max 80% of P
> > 
> > \> \>\>\>\>\>\>\> For X in main = Per ( Closest whole number or half that equals closest  
> > \> \>\>\>\>\>\>\> to but greater than Per)  
> > \> \>\>\>\>\>\>\> main = (( 5 \* 8)/10)\*100
> > 
> > \> \>\>\>\>\>\>\> So in example intially main equalled 400%. And answer I would want to  
> > \> \>\>\>\>\>\>\> resolve it to is X = 4 which is 200% as 3.50 equals 180%.
> > 
> > \> \>\>\>\>\>\>\> Any ideas?
> > 
> > \> \>\>\>\>\>\> Sorry you lost me somewhere along the path. Also it's tea time right now...
> > 
> > \> \>\>\>\>\>\> Cheers
> > 
> > \> \>\>\>\>\>\> robert
> > 
> > \> \>\>\>\>\>\> --  
> > \> \>\>\>\>\>\> remember.guy do |as, often| as.you\_can - without endhttp://blog.rubybestpractices.com/
> > 
> > \> \>\>\>\>\> So I want to check by changing X when in forumla "main" that it is =\>  
> > \> \>\>\>\>\> than "per"
> > 
> > \> \>\>\>\>\> In simple terms I want to calculate units needed to reach a rate of  
> > \> \>\>\>\>\> return, X represents the variable units and per is the ROI(return of  
> > \> \>\>\>\>\> investment rate I would deaire to acheive), R is the ratio of return  
> > \> \>\>\>\>\> and P is a pool or base amount, I am using base 10 to start off with.
> > 
> > \> \>\>\>\>\> I am trying to test X for a value, the only constraint on X is that it  
> > \> \>\>\>\>\> cannot exceed 80% of the P or Pool amount.  
> > \> \>\>\>\>\> So if I set per = 190%
> > 
> > \> \>\>\>\>\> R = 5  
> > \> \>\>\>\>\> P = 10  
> > \> \>\>\>\>\> Per = 190  
> > \> \>\>\>\>\> X = max 80% of P
> > 
> > \> \>\>\>\>\> For X in main \>= per
> > 
> > \> \>\>\>\>\> main = (( 5 \* X)/10)\*100 \>= 190%
> > 
> > \> \>\>\>\>\> so for X = 80% of P or 8 base units
> > 
> > \> \>\>\>\>\> main = (( 5 \* 8)/10)\*100
> > 
> > \> \>\>\>\>\> which would test out as
> > 
> > \> \>\>\>\>\> main \>= per
> > 
> > \> \>\>\>\>\> 400 =\> 190 -
> > 
> > \> \>\>\>\>\> So when X is 8 units the main section is greater than per but its not  
> > \> \>\>\>\>\> the closest whole unit to per.
> > 
> > \> \>\>\>\>\> So when X = 40% or 4 base units
> > 
> > \> \>\>\>\>\> main = (( 5 \* 4)/10)\*100  
> > \> \>\>\>\>\> main \>= per  
> > \> \>\>\>\>\> 200 \>= 190
> > 
> > \> \>\>\>\>\> this is the largest unit in 0.5 increments that remains greater than  
> > \> \>\>\>\>\> Per of 190 so I would want X once tested to resolve to this.
> > 
> > \> \>\>\>\>\> I hope that made sense.
> > 
> > \> \>\>\>\> So how do I best get X to run a loop in 0.5 increments until it  
> > \> \>\>\>\> reaches the closest value that makes the left side of an equation  
> > \> \>\>\>\> greater or equal to the right. But where it is the lowest value that  
> > \> \>\>\>\> is greater than or equal to the the right.
> > 
> > \> \>\>\>\> Main =\> Per - where main is the lowest value it can be greater than  
> > \> \>\>\>\> per.
> > 
> > \> \>\>\>\> Cheers
> > 
> > \> \>\>\>\> Sayth
> > 
> > \> \>\>\> Something's not right with your formulas (I suspect)
> > 
> > \> \>\>\>\>\>\>\> main = (( R \* X)/P)\*100
> > 
> > \> \>\>\> and X = X\*P
> > 
> > \> \>\>\> so... main =  
> > \> \>\>\> R\*(X\*P)  
> > \> \>\>\> ----------- \* 100  
> > \> \>\>\> P
> > 
> > \> \>\>\> the P's cancel out...  
> > \> \>\>\> R\*X\*100
> > 
> > \> \>\>\> varying P will not change the results of your calculations...
> > 
> > \> \>\>\> here's what I believe that you're asking for...  
> > \> \>\>\> ================================  
> > \> \>\>\> r = 5.0  
> > \> \>\>\> p = 10.0  
> > \> \>\>\> per = 190.0  
> > \> \>\>\> x = 0.8
> > 
> > \> \>\>\> begin  
> > \> \>\>\> main = ((r\*(x\*p))/p)\*100  
> > \> \>\>\> lastSuccess = x if main \>= per  
> > \> \>\>\> x -= 0.05  
> > \> \>\>\> end while main \>= per  
> > \> \>\>\> puts "#{lastSuccess\*100.0}%"  
> > \> \>\>\> ===============================  
> > \> \>\>\> note: you can change p al day long and always get the same answer
> > 
> > \> \>\>\> there are probably more "rubified" ways to express this.  
> > \> \>\>\> I've tried to maintain the approach you've stated. However, I would simplify the equation first, and since the last X that succeeds as you decrement is the same thing as the first that succeeds as you're going up, I'd probably turn t into something like...  
> > \> \>\>\> ===============================  
> > \> \>\>\> r = 5.0  
> > \> \>\>\> p = 10.0  
> > \> \>\>\> per = 190.0  
> > \> \>\>\> max\_x = 0.8
> > 
> > \> \>\>\> x = 0.05  
> > \> \>\>\> const = r\*100 # simplified without X  
> > \> \>\>\> x += 0.05 while (x\*const \< per) && (x \<= max\_x)  
> > \> \>\>\> puts x \<= max\_x ? "#{lastSuccess\*100.0}%" : "no answer"  
> > \> \>\>\> ===============================
> > 
> > \> \>\>\> Mike Cargal
> > 
> > \> \>\>\> m...@cargal.nethttp://blog.mikecargal.com
> > 
> > \> \>\> Thanks for looking at this for me.
> > 
> > \> \>\>\> You've really lost me when 80% turns into 8 (or 8 base units). What is a base unit? And why would it be equal to 10%?  
> > \> \>\>\> Are you looking to vary X from 80% down by 5% increments?
> > 
> > \> \>\> This test is the first of 4 I plan to make into one program. For each  
> > \> \>\> their while be a maximum allocation so in this case 80% so may be 40%  
> > \> \>\> etc. The pool in future will vary but I am using base 10 while I write  
> > \> \>\> it(hoping it would be clearer for another person reading it). So that  
> > \> \>\> means that X has a max allocation of units if 40% was the maximum  
> > \> \>\> allocation and base 10 then X would be 4 units and I would want my  
> > \> \>\> loop to test X from 0 to 4 in 0.5 increments.
> > 
> > \> \>\> I am going to need more time to read your solution as I haven't got it  
> > \> \>\> first read. I had started to look athttp://www.rubyist.net/~slagell/ruby/iterators.html
> > 
> > \> \>\> and a solution flow similar to
> > 
> > \> \>\> \> def WHILE(cond)  
> > \> \>\> \> return if not cond  
> > \> \>\> \> yield  
> > \> \>\> \> retry  
> > \> \>\> \> end  
> > \> \>\> nil  
> > \> \>\> \> i=0; WHILE(i\<3) { print i; i+=1 }  
> > \> \>\> 012 nil- Hide quoted text -
> > 
> > \> \>\> - Show quoted text -
> > 
> > \> \> Actually after re-reading your post I get it. All but one small bit.
> > 
> > \> \> In your formula how does the ? "#{lastSuccess\*100.0}%" bit work? what  
> > \> \> data does lastSuccess pull.
> > 
> > \> \> x = 0.05  
> > \> \> const = r\*100 # simplified without X  
> > \> \> x += 0.05 while (x\*const \< per) && (x \<= max\_x)  
> > \> \> puts x \<= max\_x ? "#{lastSuccess\*100.0}%" : "no answer"
> > 
> > \> my bad...
> > 
> > \> r = 5.0  
> > \> p = 10.0  
> > \> per = 190.0  
> > \> max\_x = 0.8
> > 
> > \> x = 0.05  
> > \> const = r\*100 # simplified without X  
> > \> x += 0.05 while (x\*const \< per) && (x \<= max\_x)  
> > \> puts x \<= max\_x ? "#{x\*100.0}%" : "no answer"
> > 
> > \> Mike Cargal
> > 
> > \> m...@cargal.nethttp://blog.mikecargal.com  
> > \> I still think we first need to get the math correct before we can come  
> > \> up with solutions. Formulas I have seen in this thread look like they  
> > \> could be solved with some simple transformations and do not need any  
> > \> nested intervals or similar approximation algorithms. So far I find the  
> > \> problem description quite confusing.
> > 
> > Essentially without formulas all I am calculating is how many units at  
> > a specified rate of return it would take to reach a percentage of rate  
> > of return. Only additionally I have specified a maximum unit  
> > allocation. There would ultimately be several options with different  
> > return rates and max allocations from each pool. A pool is a  
> > percentage subset of a bank.
> > 
> > \> x = 0.05  
> > \> const = r\*100 # simplified without X  
> > \> x += 0.05 while (x\*const \< per) && (x \<= max\_x)  
> > \> puts x \<= max\_x ? "#{x\*100.0}%" : "no answer"
> > 
> > Mikes solution definitely works for working units(X)..thank you. I  
> > need to set constants and obtain user inputs for different scenarios  
> > and call it to the function.
> > 
> > \> could be solved with some simple transformations and do not need any
> > 
> > I need to lookup what a transformation is...

---

<div class="post-metadata">

### Author: ![flebber](https://avatars.discourse-cdn.com/v4/letter/f/f9ae1b/32.png) [@flebber](https://rubytalk.org/u/flebber)
#### Post date: [10 November 2010 01:55 UTC](https://rubytalk.org/t/create-a-class-ideas/60746/15 "2010-11-10T01:55:14Z")

</div>

upated.

Bank = $500.00 # later to be a running total calculated

def ROIcalc  
const = r\*100 # simplified without X  
x += 0.05 while (x\*const \< per) && (x \<= max\_x)  
puts x \<= max\_x ? "#{x\*100.0}%" : "no answer"  
end

def pool  
if Bank \> 200 then Bank \* 0.05  
Else $10.00  
end

# Scenario 1  
a = ROIcalc(per = 190, max\_x = 0.8, puts "What ratio of return do you  
expect?" r = gets.chomp, pool)  
b = ROIcalc(per = 200, max\_x = 0.4, r = 12.00, pool)

if a + b \> 10 then err = 10.00 - ( a + b) puts "Your exceeding your  
pool by" + err.to\_s  
else a + b \< 10 then readd = 10.00 - ( a + b)  
&nbsp;&nbsp;&nbsp;&nbsp;# then use readd to reassign extra units in ratio 75/25 to a and b  
end

> **···**
>
> On Nov 10, 12:44 pm, flebber \<flebber.c...@gmail.com\> wrote:
> 
> > On Nov 8, 9:40 pm, flebber \<flebber.c...@gmail.com\> wrote:
> > 
> > \> On Nov 8, 3:33 am, Mike Cargal \<m...@cargal.net\> wrote:
> > 
> > \> \> On Nov 7, 2010, at 12:20 AM, flebber wrote:
> > 
> > \> \> \> On Nov 7, 12:13 pm, flebber \<flebber.c...@gmail.com\> wrote:  
> > \> \> \>\> On Nov 7, 11:06 am, Mike Cargal \<m...@cargal.net\> wrote:
> > 
> > \> \> \>\>\> On Nov 6, 2010, at 7:00 PM, flebber wrote:
> > 
> > \> \> \>\>\>\> On Nov 7, 9:45 am, flebber \<flebber.c...@gmail.com\> wrote:  
> > \> \> \>\>\>\>\> On Nov 7, 2:24 am, Robert Klemme \<shortcut...@googlemail.com\> wrote:
> > 
> > \> \> \>\>\>\>\>\> On 06.11.2010 10:19, flebber wrote:
> > 
> > \> \> \>\>\>\>\>\>\> I am trying to create a class. I am struggling to figure the best flow  
> > \> \> \>\>\>\>\>\>\> to get the maths side to work.
> > 
> > \> \> \>\>\>\>\>\>\> So say that
> > 
> > \> \> \>\>\>\>\>\>\> R is a float given by user input  
> > \> \> \>\>\>\>\>\>\> P is a Total amount(Pool)  
> > \> \> \>\>\>\>\>\>\> Per is a variable %  
> > \> \> \>\>\>\>\>\>\> X is a variable that is a percentage of P defined by a maximum  
> > \> \> \>\>\>\>\>\>\> allocation.
> > 
> > \> \> \>\>\>\>\>\>\> So main = (( R \* X)/P)\*100
> > 
> > \> \> \>\>\>\>\>\> First of all you should get your variables right. Variable "Per" does  
> > \> \> \>\>\>\>\>\> not show up in the formula and "main" is not mentioned in the list.
> > 
> > \> \> \>\>\>\>\>\> The meaning of the formula is totally unclear to me. From what you gave  
> > \> \> \>\>\>\>\>\> you are calculating the fraction (R/P) multiply it with 100 (so you  
> > \> \> \>\>\>\>\>\> actually get (R/P) percent and now you multiply with another percentage  
> > \> \> \>\>\>\>\>\> (X). So you have a percentage of a percentage.
> > 
> > \> \> \>\>\>\>\>\>\> What I want to test is the value of X needed to equal Per from X's  
> > \> \> \>\>\>\>\>\>\> maximum allocation down.
> > 
> > \> \> \>\>\>\>\>\> Can you write down a formula that contains all variables in your list  
> > \> \> \>\>\>\>\>\> and point at the fixed ones (constants), user inputs and variables you  
> > \> \> \>\>\>\>\>\> want to resolve?
> > 
> > \> \> \>\>\>\>\>\>\> What i am thinking but cant get right
> > 
> > \> \> \>\>\>\>\>\>\> Say
> > 
> > \> \> \>\>\>\>\>\>\> R = 5  
> > \> \> \>\>\>\>\>\>\> P = 10  
> > \> \> \>\>\>\>\>\>\> Per = 190  
> > \> \> \>\>\>\>\>\>\> X = max 80% of P
> > 
> > \> \> \>\>\>\>\>\>\> For X in main = Per ( Closest whole number or half that equals closest  
> > \> \> \>\>\>\>\>\>\> to but greater than Per)  
> > \> \> \>\>\>\>\>\>\> main = (( 5 \* 8)/10)\*100
> > 
> > \> \> \>\>\>\>\>\>\> So in example intially main equalled 400%. And answer I would want to  
> > \> \> \>\>\>\>\>\>\> resolve it to is X = 4 which is 200% as 3.50 equals 180%.
> > 
> > \> \> \>\>\>\>\>\>\> Any ideas?
> > 
> > \> \> \>\>\>\>\>\> Sorry you lost me somewhere along the path. Also it's tea time right now...
> > 
> > \> \> \>\>\>\>\>\> Cheers
> > 
> > \> \> \>\>\>\>\>\> robert
> > 
> > \> \> \>\>\>\>\>\> --  
> > \> \> \>\>\>\>\>\> remember.guy do |as, often| as.you\_can - without endhttp://blog.rubybestpractices.com/
> > 
> > \> \> \>\>\>\>\> So I want to check by changing X when in forumla "main" that it is =\>  
> > \> \> \>\>\>\>\> than "per"
> > 
> > \> \> \>\>\>\>\> In simple terms I want to calculate units needed to reach a rate of  
> > \> \> \>\>\>\>\> return, X represents the variable units and per is the ROI(return of  
> > \> \> \>\>\>\>\> investment rate I would deaire to acheive), R is the ratio of return  
> > \> \> \>\>\>\>\> and P is a pool or base amount, I am using base 10 to start off with.
> > 
> > \> \> \>\>\>\>\> I am trying to test X for a value, the only constraint on X is that it  
> > \> \> \>\>\>\>\> cannot exceed 80% of the P or Pool amount.  
> > \> \> \>\>\>\>\> So if I set per = 190%
> > 
> > \> \> \>\>\>\>\> R = 5  
> > \> \> \>\>\>\>\> P = 10  
> > \> \> \>\>\>\>\> Per = 190  
> > \> \> \>\>\>\>\> X = max 80% of P
> > 
> > \> \> \>\>\>\>\> For X in main \>= per
> > 
> > \> \> \>\>\>\>\> main = (( 5 \* X)/10)\*100 \>= 190%
> > 
> > \> \> \>\>\>\>\> so for X = 80% of P or 8 base units
> > 
> > \> \> \>\>\>\>\> main = (( 5 \* 8)/10)\*100
> > 
> > \> \> \>\>\>\>\> which would test out as
> > 
> > \> \> \>\>\>\>\> main \>= per
> > 
> > \> \> \>\>\>\>\> 400 =\> 190 -
> > 
> > \> \> \>\>\>\>\> So when X is 8 units the main section is greater than per but its not  
> > \> \> \>\>\>\>\> the closest whole unit to per.
> > 
> > \> \> \>\>\>\>\> So when X = 40% or 4 base units
> > 
> > \> \> \>\>\>\>\> main = (( 5 \* 4)/10)\*100  
> > \> \> \>\>\>\>\> main \>= per  
> > \> \> \>\>\>\>\> 200 \>= 190
> > 
> > \> \> \>\>\>\>\> this is the largest unit in 0.5 increments that remains greater than  
> > \> \> \>\>\>\>\> Per of 190 so I would want X once tested to resolve to this.
> > 
> > \> \> \>\>\>\>\> I hope that made sense.
> > 
> > \> \> \>\>\>\> So how do I best get X to run a loop in 0.5 increments until it  
> > \> \> \>\>\>\> reaches the closest value that makes the left side of an equation  
> > \> \> \>\>\>\> greater or equal to the right. But where it is the lowest value that  
> > \> \> \>\>\>\> is greater than or equal to the the right.
> > 
> > \> \> \>\>\>\> Main =\> Per - where main is the lowest value it can be greater than  
> > \> \> \>\>\>\> per.
> > 
> > \> \> \>\>\>\> Cheers
> > 
> > \> \> \>\>\>\> Sayth
> > 
> > \> \> \>\>\> Something's not right with your formulas (I suspect)
> > 
> > \> \> \>\>\>\>\>\>\> main = (( R \* X)/P)\*100
> > 
> > \> \> \>\>\> and X = X\*P
> > 
> > \> \> \>\>\> so... main =  
> > \> \> \>\>\> R\*(X\*P)  
> > \> \> \>\>\> ----------- \* 100  
> > \> \> \>\>\> P
> > 
> > \> \> \>\>\> the P's cancel out...  
> > \> \> \>\>\> R\*X\*100
> > 
> > \> \> \>\>\> varying P will not change the results of your calculations...
> > 
> > \> \> \>\>\> here's what I believe that you're asking for...  
> > \> \> \>\>\> ================================  
> > \> \> \>\>\> r = 5.0  
> > \> \> \>\>\> p = 10.0  
> > \> \> \>\>\> per = 190.0  
> > \> \> \>\>\> x = 0.8
> > 
> > \> \> \>\>\> begin  
> > \> \> \>\>\> main = ((r\*(x\*p))/p)\*100  
> > \> \> \>\>\> lastSuccess = x if main \>= per  
> > \> \> \>\>\> x -= 0.05  
> > \> \> \>\>\> end while main \>= per  
> > \> \> \>\>\> puts "#{lastSuccess\*100.0}%"  
> > \> \> \>\>\> ===============================  
> > \> \> \>\>\> note: you can change p al day long and always get the same answer
> > 
> > \> \> \>\>\> there are probably more "rubified" ways to express this.  
> > \> \> \>\>\> I've tried to maintain the approach you've stated. However, I would simplify the equation first, and since the last X that succeeds as you decrement is the same thing as the first that succeeds as you're going up, I'd probably turn t into something like...  
> > \> \> \>\>\> ===============================  
> > \> \> \>\>\> r = 5.0  
> > \> \> \>\>\> p = 10.0  
> > \> \> \>\>\> per = 190.0  
> > \> \> \>\>\> max\_x = 0.8
> > 
> > \> \> \>\>\> x = 0.05  
> > \> \> \>\>\> const = r\*100 # simplified without X  
> > \> \> \>\>\> x += 0.05 while (x\*const \< per) && (x \<= max\_x)  
> > \> \> \>\>\> puts x \<= max\_x ? "#{lastSuccess\*100.0}%" : "no answer"  
> > \> \> \>\>\> ===============================
> > 
> > \> \> \>\>\> Mike Cargal
> > 
> > \> \> \>\>\> m...@cargal.nethttp://blog.mikecargal.com
> > 
> > \> \> \>\> Thanks for looking at this for me.
> > 
> > \> \> \>\>\> You've really lost me when 80% turns into 8 (or 8 base units). What is a base unit? And why would it be equal to 10%?  
> > \> \> \>\>\> Are you looking to vary X from 80% down by 5% increments?
> > 
> > \> \> \>\> This test is the first of 4 I plan to make into one program. For each  
> > \> \> \>\> their while be a maximum allocation so in this case 80% so may be 40%  
> > \> \> \>\> etc. The pool in future will vary but I am using base 10 while I write  
> > \> \> \>\> it(hoping it would be clearer for another person reading it). So that  
> > \> \> \>\> means that X has a max allocation of units if 40% was the maximum  
> > \> \> \>\> allocation and base 10 then X would be 4 units and I would want my  
> > \> \> \>\> loop to test X from 0 to 4 in 0.5 increments.
> > 
> > \> \> \>\> I am going to need more time to read your solution as I haven't got it  
> > \> \> \>\> first read. I had started to look athttp://www.rubyist.net/~slagell/ruby/iterators.html
> > 
> > \> \> \>\> and a solution flow similar to
> > 
> > \> \> \>\> \> def WHILE(cond)  
> > \> \> \>\> \> return if not cond  
> > \> \> \>\> \> yield  
> > \> \> \>\> \> retry  
> > \> \> \>\> \> end  
> > \> \> \>\> nil  
> > \> \> \>\> \> i=0; WHILE(i\<3) { print i; i+=1 }  
> > \> \> \>\> 012 nil- Hide quoted text -
> > 
> > \> \> \>\> - Show quoted text -
> > 
> > \> \> \> Actually after re-reading your post I get it. All but one small bit.
> > 
> > \> \> \> In your formula how does the ? "#{lastSuccess\*100.0}%" bit work? what  
> > \> \> \> data does lastSuccess pull.
> > 
> > \> \> \> x = 0.05  
> > \> \> \> const = r\*100 # simplified without X  
> > \> \> \> x += 0.05 while (x\*const \< per) && (x \<= max\_x)  
> > \> \> \> puts x \<= max\_x ? "#{lastSuccess\*100.0}%" : "no answer"
> > 
> > \> \> my bad...
> > 
> > \> \> r = 5.0  
> > \> \> p = 10.0  
> > \> \> per = 190.0  
> > \> \> max\_x = 0.8
> > 
> > \> \> x = 0.05  
> > \> \> const = r\*100 # simplified without X  
> > \> \> x += 0.05 while (x\*const \< per) && (x \<= max\_x)  
> > \> \> puts x \<= max\_x ? "#{x\*100.0}%" : "no answer"
> > 
> > \> \> Mike Cargal
> > 
> > \> \> m...@cargal.nethttp://blog.mikecargal.com  
> > \> \> I still think we first need to get the math correct before we can come  
> > \> \> up with solutions. Formulas I have seen in this thread look like they  
> > \> \> could be solved with some simple transformations and do not need any  
> > \> \> nested intervals or similar approximation algorithms. So far I find the  
> > \> \> problem description quite confusing.
> > 
> > \> Essentially without formulas all I am calculating is how many units at  
> > \> a specified rate of return it would take to reach a percentage of rate  
> > \> of return. Only additionally I have specified a maximum unit  
> > \> allocation. There would ultimately be several options with different  
> > \> return rates and max allocations from each pool. A pool is a  
> > \> percentage subset of a bank.
> > 
> > \> \> x = 0.05  
> > \> \> const = r\*100 # simplified without X  
> > \> \> x += 0.05 while (x\*const \< per) && (x \<= max\_x)  
> > \> \> puts x \<= max\_x ? "#{x\*100.0}%" : "no answer"
> > 
> > \> Mikes solution definitely works for working units(X)..thank you. I  
> > \> need to set constants and obtain user inputs for different scenarios  
> > \> and call it to the function.
> > 
> > \> \> could be solved with some simple transformations and do not need any
> > 
> > \> I need to lookup what a transformation is...
> > 
> > This is where I was headed with it....any suggestions apprecaited.
> > 
> > Bank = $500.00 # later to be a running total calculated
> > 
> > def ROIcalc  
> > const = r\*100 # simplified without X  
> > x += 0.05 while (x\*const \< per) && (x \<= max\_x)  
> > puts x \<= max\_x ? "#{x\*100.0}%" : "no answer"  
> > end
> > 
> > def pool  
> > if Bank \> 200 then Bank \* 0.05  
> > Else 10.00  
> > end
> > 
> > # Scenario 1  
> > a = ROIcalc(per = 190, max\_x = 0.8, puts "What ratio of return do you  
> > expect?" r = gets.chomp, pool)  
> > b = ROIcalc(per = 200, max\_x = 0.4, r = 12.00, pool)  
> > # output result if valid  
> > # or return no valid result choose another scenario or end.
> > 
> > This is where I was headed.
> > 
> > Bank = $500.00 # later to be a running total calculated
> > 
> > def ROIcalc  
> > const = r\*100 # simplified without X  
> > x += 0.05 while (x\*const \< per) && (x \<= max\_x)  
> > puts x \<= max\_x ? "#{x\*100.0}%" : "no answer"  
> > end
> > 
> > def pool  
> > if Bank \> 200 then Bank \* 0.05  
> > Else $10.00  
> > end
> > 
> > # Scenario 1  
> > a = ROIcalc(per = 190, max\_x = 0.8, puts "What ratio of return do you  
> > expect?" r = gets.chomp, pool)  
> > b = ROIcalc(per = 200, max\_x = 0.4, r = 12.00, pool)  
> > # output result if valid  
> > # or return no valid result choose another scenario or end.
