Advertisement

Multiplier.

Started by February 09, 2002 03:01 PM
13 comments, last by leggyguy 23 years ago
I am not sure of the name of the functions you need to use, since I use vb, but why not

1)Use a string split function on the "."
2)Find the length of the string after the "."
3)Multiply the number by 10^Length

Hope that helps
Turring Machines are better than C++ any day ^_~
It seems that your question is getting confused, leggyguy. basically, all you want is ceil(x), but you want to find it with multiplication?

EG:

f(2) = 2
f(2.0001) = 3
f(2.9999) = 3
f(3) = 3
f(14.3) = 15

etc, etc

It sounds like people are trying to achieve something else.
Advertisement
He doesn''t want that TerranFury, we wants the smallest number that that number fits into. so, ceil() isn''t what he wants. As he said, 1.5 becomes 3.0, 1.25 becomes 5.0 etc...

Intrest86 - your method also does not find the smallest number, only a number. He might aswell just multiply it my a million in that case ( single precision floats have 6 d.p. ).

Death of one is a tragedy, death of a million is just a statistic.
If at first you don't succeed, redefine success.
Dohhhh...

Sorry bout that, I didn''t see that ''smallest'' part.

-potential energy is easily made kinetic-

-potential energy is easily made kinetic-

Ok, I think I have it for good now. I was half right before, it does involve reciprocals. I did the easy ones before, so I won''t do it again here. Basically you repeat the steps that I showed earlier until there is a whole number left, then you multiply back to get the final answer.

So, lets take 1.6:

Decimal = 0.6 -> Recip = 5/3
Decimal = 2/3 -> Recip = 3/2
Decimal = 0.5 -> Recip = 2

Now we multiply back:

( 2 * 3/2 * 5/3 ) * 1.6 = 8

Lets have another example: 1.3
Decimal = 0.3 -> Recip = 10/3
Decimal = 1/3 -> Recip = 3

Muliply back:

( 3 * 10/3 ) * 1.3 = 13

Lets do a more complicated one now: 1.67

Decimal = 0.67 -> Recip = 100/67
Decimal = 33/67 -> Recip = 67/33
Decimal = 34/33 -> Recip = 33/34
Decimal = 33/34 -> Recip = 34/33
Decimal = 1/33 -> Recip = 33

Multiply back:

( 33 * 34/33 * 33/34 * 67/33 * 100/67 ) * 1.67 = 167.

I''m seeing a pattern here. Note how thoot said that if the last decimal is odd multiply by 10, and if it''s even multiply by 5, well, I can see that, just to different powers of 10. I the last example, the last decimal was odd but at the 100th position, therefore the multiple was 100. Lets try one ending with an even number and to the 1000th place:

1.566 - This should be multiplied by 5000 then, lets try it!

1.566 * 5000 = 7830.

There is a problem here, the zero on the end is not needed. Therefore you''d think the lowest number to be no higher than 783. Lets do it the other way to see what we get.

.566 -> 500/283
217/283 -> 283/217
66/217 -> 217/66
151/66 -> 66/151
66/151 -> 151/66
85/66 -> 66/85
66/85 -> 85/66
19/66 -> 66/19
47/19 -> 19/47
19/47 -> 47/19
28/19 -> 19/28
19/28 -> 28/19
9/19 -> 19/9
10/9 -> 9/10
9/10 -> 10/9
1/10 -> 10

Now you can cancel all up that list, except that last 10, which you cannot cancel, which ends up multiplying the 500 to 5000. Now, I have a problem with that. The lowest possible answer is 500 * 1.566 not 5000 * 1.566.

So, this is what you would have to do:

If the last decimal place is odd, multiply by 10*(1/decimal_order).

If the last decimal place is even, multiply by 5*(1/decimal_order).

Where decimal_order is 1/100, 1/1000, 1/1000000 etc.

If the answer you get after that is a multiple of 10, divide by 10 until it nolonger is.

In C++, this would look something like:

  int GetLowestMultiple( double number ){    // we need to convert to a string to get the last decimal in the number    char string[32];    memset( string, 0, 32 );     int decimal_point = 0;    int sign = 0;    // we pass 15 to the function because the double data type has a maximum of 15 digits    // of precision.    string =  _fcvt( number, 15, &decimal_point, &sign );    // now we loop through the string array starting from the 15th place after the decimal point    // and moving in, we stop when we see a number that is not 0, this will be the last     // decimal place.    for( int i = decimal_point + 15; i > decimal_point; i-- )    {        if( string[i] != ''0'' )            break;    }    // check to see we aren''t at the decimal point    if( i == decimal_point )    {        // the number passed in was all ready fine, so return a multiple of 1        return 1;    }    // we have the last decimal place offset from decimal_point held in i. We need this last    // number, and it''s decimal order. decimal order is calculated by 10^( i - decimal point ).    // this can simply be calculated by multiplying 1 by 10 for i-decimal point times.    int last_decimal_offset = decimal_point - i;    int decimal_order = 1;    for( j = 0; j < i; j++ )    {        decimal_order *= 10;    }    // now we need the last decimal place in an actual integer so we can play with it.    int last_decimal = atoi( string[i] );    // we now need to know whether it is even or odd. This can be accomplished by dividing    // it by 2, and seeing if there is a remainder, if there is, it''s odd, if not it''s even. This will    // not work if it is one, so we need to handle that as a special case.    int sign = last_decimal % 2;    if( last_decimal == 1 )    {        int raw_answer = ( int )( number * 10 * decimal_order );        while( raw_answer % 10 )            raw_answer /= 10;        return raw_answer;    }    else    {        if( sign )        {            // odd            int raw_answer = ( int )( number * 10 * decimal_order );            while( raw_answer % 10 )                raw_answer /= 10;            return raw_answer;        }        else        {            // even            int raw_answer = ( int )( number * 5 * decimal_order );            while( raw_answer % 10 )                raw_answer /= 10;            return raw_answer;        }    }}  


Please note that I have no tested this code, and in all probability it won''t work.

Death of one is a tragedy, death of a million is just a statistic.
If at first you don't succeed, redefine success.

This topic is closed to new replies.

Advertisement