# Math Question (I feel stupid)

**URL:** <https://boards.straightdope.com/t/math-question-i-feel-stupid/77522>\
**Category:** Factual Questions\
**Created:** [August 22, 2001, 12:18pm UTC](https://boards.straightdope.com/t/math-question-i-feel-stupid/77522 "2001-08-22T12:18:21Z")\
**Posts on this page:** 10\
**Page:** 1

<div class="post-metadata">

**Author:** ![AHunter3](https://sea3.discourse-cdn.com/straightdope/user_avatar/boards.straightdope.com/ahunter3/32/368_2.png) [@AHunter3](https://boards.straightdope.com/u/AHunter3)\
**Post date:** [August 22, 2001, 12:18pm UTC](https://boards.straightdope.com/t/math-question-i-feel-stupid/77522/1 "2001-08-22T12:18:21Z")

</div>

I should know this. I’m shocked at myself for drawing a blank.

I have some numbers, each of which is 2 to some power, i.e.,

2  
32  
8  
256  
64  
I need a formula that returns the power of two that any such number is, i.e.,

1  
5  
3  
8  
6

---

<div class="post-metadata">

**Author:** ![kabbes](https://avatars.discourse-cdn.com/v4/letter/k/e99b99/32.png) [@kabbes](https://boards.straightdope.com/u/kabbes)\
**Post date:** [August 22, 2001, 12:25pm UTC](https://boards.straightdope.com/t/math-question-i-feel-stupid/77522/2 "2001-08-22T12:25:19Z")

</div>

Let _y_ be the power of 2. Then _y_ = 2[sup]_x_[/sup]

ln _y_ = _x_\* ln 2

So _x_ = ln 2 / ln _y_

pan

---

<div class="post-metadata">

**Author:** ![Dr.Lao](https://avatars.discourse-cdn.com/v4/letter/d/2bfe46/32.png) [@Dr.Lao](https://boards.straightdope.com/u/Dr.Lao)\
**Post date:** [August 22, 2001, 12:26pm UTC](https://boards.straightdope.com/t/math-question-i-feel-stupid/77522/3 "2001-08-22T12:26:00Z")

</div>

Logrithms.

log[sub]2[/sub] 32 = 5.

Since most calculators don’t have base 2 logs, you can find it this way:

(log[sub]10[/sub] 32) / (log[sub]10[/sub] 2) = 5

---

<div class="post-metadata">

**Author:** ![kabbes](https://avatars.discourse-cdn.com/v4/letter/k/e99b99/32.png) [@kabbes](https://boards.straightdope.com/u/kabbes)\
**Post date:** [August 22, 2001, 12:26pm UTC](https://boards.straightdope.com/t/math-question-i-feel-stupid/77522/4 "2001-08-22T12:26:21Z")

</div>

Oops!

I mean of course

_x_ = ln _y_ / ln 2

Now of course I have to wait 60 seconds to submit…

pan

---

<div class="post-metadata">

**Author:** ![AHunter3](https://sea3.discourse-cdn.com/straightdope/user_avatar/boards.straightdope.com/ahunter3/32/368_2.png) [@AHunter3](https://boards.straightdope.com/u/AHunter3)\
**Post date:** [August 22, 2001, 2:13pm UTC](https://boards.straightdope.com/t/math-question-i-feel-stupid/77522/5 "2001-08-22T14:13:18Z")

</div>

Thanks, y’all!

I was doing a function in which users could select any of a series of fields – set up to appear as rows, but actually in the same record (long story)-- and since the consecutive order was hard-wired, I made a simple additive formula [If checkbox is on, 1+sum of previous item checkboxvalues] that gave me an elegant in which the first checked item is 1, second 2, third 4, fourth 8, and so on. “Elegant!”, I said to myself! “Now to calculate the row number (leaving out the unchecked items), all I have to do is…uh…you know, take the…um, whatchamacallit, the…”

:o

Thanks again!

---

<div class="post-metadata">

**Author:** ![Achernar](https://avatars.discourse-cdn.com/v4/letter/a/e274bd/32.png) [@Achernar](https://boards.straightdope.com/u/Achernar)\
**Post date:** [August 22, 2001, 2:50pm UTC](https://boards.straightdope.com/t/math-question-i-feel-stupid/77522/6 "2001-08-22T14:50:50Z")

</div>

The answers already given are great, and you’d probably do just fine with them. But if you’re doing this as part of a program, you may want to stick with integer arithmetic. In C, it would be something like this:

```auto

int LogBase2 = 0;
while (InputNumber >>= 1) LogBase2++;

```

I think…

---

<div class="post-metadata">

**Author:** ![ultrafilter](https://avatars.discourse-cdn.com/v4/letter/u/3d9bf3/32.png) [@ultrafilter](https://boards.straightdope.com/u/ultrafilter)\
**Post date:** [August 23, 2001, 11:44pm UTC](https://boards.straightdope.com/t/math-question-i-feel-stupid/77522/7 "2001-08-23T23:44:27Z")

</div>

> [@](#):
>
> \*Originally posted by Achernar \*  
> \*\*The answers already given are great, and you’d probably do just fine with them. But if you’re doing this as part of a program, you may want to stick with integer arithmetic. In C, it would be something like this:
> 
> ```auto
> 
> int LogBase2 = 0;
> while (InputNumber >>= 1) LogBase2++;
> 
> ```
> 
> I think…\*\*

Might want to divide the number by two on every iteration.

---

<div class="post-metadata">

**Author:** ![Punoqllads](https://avatars.discourse-cdn.com/v4/letter/p/d2c977/32.png) [@Punoqllads](https://boards.straightdope.com/u/Punoqllads)\
**Post date:** [August 23, 2001, 11:56pm UTC](https://boards.straightdope.com/t/math-question-i-feel-stupid/77522/8 "2001-08-23T23:56:28Z")

</div>

> [@](#):
>
> \*Originally posted by ultrafilter \*
> 
> > [@](#):
> >
> > \*Originally posted by Achernar \*  
> > \*\*The answers already given are great, and you’d probably do just fine with them. But if you’re doing this as part of a program, you may want to stick with integer arithmetic. In C, it would be something like this:
> > 
> > ```auto
> > 
> > int LogBase2 = 0;
> > while (InputNumber >>= 1) LogBase2++;
> > 
> > ```
> > 
> > I think…\*\*
> 
> Might want to divide the number by two on every iteration.

He is. That’s what the \>\>= operator does. The only problem I see with it is that it won’t terminate if InputNumber is negative.

---

<div class="post-metadata">

**Author:** ![ultrafilter](https://avatars.discourse-cdn.com/v4/letter/u/3d9bf3/32.png) [@ultrafilter](https://boards.straightdope.com/u/ultrafilter)\
**Post date:** [August 24, 2001, 12:15am UTC](https://boards.straightdope.com/t/math-question-i-feel-stupid/77522/9 "2001-08-24T00:15:10Z")

</div>

> [@](#):
>
> \*Originally posted by Punoqllads \*  
> \*\*
> 
> > [@](#):
> >
> > \*Originally posted by ultrafilter \*
> > 
> > > [@](#):
> > >
> > > \*Originally posted by Achernar \*  
> > > \*\*The answers already given are great, and you’d probably do just fine with them. But if you’re doing this as part of a program, you may want to stick with integer arithmetic. In C, it would be something like this:
> > > 
> > > ```auto
> > > 
> > > int LogBase2 = 0;
> > > while (InputNumber >>= 1) LogBase2++;
> > > 
> > > ```
> > > 
> > > I think…\*\*
> > 
> > Might want to divide the number by two on every iteration.
> 
> He is. That’s what the \>\>= operator does. The only problem I see with it is that it won’t terminate if InputNumber is negative.\*\*

Right, I knew that. I just don’t see the “right-shift and assign” operator often enough to recognize it. Mea culpa.

---

<div class="post-metadata">

**Author:** ![AHunter3](https://sea3.discourse-cdn.com/straightdope/user_avatar/boards.straightdope.com/ahunter3/32/368_2.png) [@AHunter3](https://boards.straightdope.com/u/AHunter3)\
**Post date:** [August 24, 2001, 2:51am UTC](https://boards.straightdope.com/t/math-question-i-feel-stupid/77522/10 "2001-08-24T02:51:35Z")

</div>

I do my programming in FileMaker. Those of you who work in C and its plusses would probably be justified in encouraging me to put “programming” in quotes, but that’s what they put on my business card.

Dr. Lao’s formula was far and away the most useful for my purposes. I defined a field ItemNameRowNum that, via base2 logarithm + the formula I described above, is the ordinal value of the chosen item if chosen (otherwise blank).

I can now have my users select from a range of choices and on the next page be presented with a list of the ones they chose for followup questions. Not a particularly exciting-sounding achievement until you consider that I did it in a single file (which in FileMaker means a single table), and with a single very portable / reusable script architecture:

Go to Layout [xx-Setup Used] Ý  
Go to Field [Row]Ý  
Set Field [CurrentRow, 1]  
Loop  
Set Field [ Case(  
ItemNameRowNum=CurrentRow, “ItemName”,  
) ] etc \*  
Set Field [CurrentRow, CurrentRow+1]  
Exit Loop If [CurrentRow=23]  
Go to Next Field  
End Loop  
Go to Layout [1.2-Training]

- I’m preserving you from the list of possible row names here. A finite list of fields they could’ve checked.

Ý xx-Setup layout contains one field, “Row”, defined as a repeating field with as many repetitions as there are choices to select from. Which makes this one of the very very few times I’ve actually used a repeating field.

This functionality more commonly entails a second table of “choosable questions”, which means it ends up contains relevant choices made as well. But for a series of complicated reasons, we need the solution to work in an environment where the file name itself will be changed in various contexts. Having a multi-file database in an environment where folks are going to be changing the file names is like having a snowball in hell. Now it is all in one neat little single-file / single-table package 🙂
