Math Question (I feel stupid)

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

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

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

Oops!

I mean of course

x = ln y / ln 2

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

pan

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!

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:


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.

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 :slight_smile: