1. ## Maths in C

I am trying to perform 2 math equations in C and send the result as a char

x = 64*(pow(2,(conversionH/256))-1); // this is 64*(2^(conversionh/256)-1)

ConversionH is a char

the next one i need to use log base 2

y = 256*(log10(1+(MapIn/64))*3.321928); // so idealy this needs to be: 256*log2(1+(mapin/64)

MapIn is a char

so my inputs and outputs are char, but this dose not seen to work. Please can you help me.
thank you.

2. if MapIn in
Code:
`MapIn/64`
is a char, then perhaps you actually want:
Code:
`MapIn/64.0`
to force the compiler to make the calculation using floating point, rather than an integer calculation.

--
Mats

3. I shal try that now. thanks

also how would i go about converting the float (result) into a char, unless the is another way to do log base 2 in C?

4. A cast from float to char is something like this:
Code:
`char c = (char) (4.2f / 1.4f);`
Which should give you a value of 3 in the char.

--
Mats

5. All log operations in C must be in base 10 ( log10() ) or log e ( log() ).

Just use the change of base logic for your log base 2 to get into log base 10.

Todd

6. this is what i've got, an ADC that dose this

Code:
```conversionH = ADC0L;           // read ADC0H data

x = (char) (64*(pow(2,(conversionH/256))-1));

MapOut = x;
SBUF0 = MapOut;           // send data H to UART```
then a DAC that dose this
Code:
```        MapIn = SBUF0;               // read data H from UART
RI0 = 0;                    // clear Receive Interrupt Flag

y =  (char) (256*(log10(1+(MapIn/64.0))*3.321928));
dacH = y;

DAC0H = dacH;               // convert digital to analogue```
it still doesn't seem to work?

7. Cast all your numeric constants, like matsp said, to float, ie, 256.0, not 256.

8. hello Todd Burch,

what do you meen by
Just use the change of base logic for your log base 2 to get into log base 10
???

hello Todd Burch,

what do you meen by ???
Like you did, log2(x) == log10(x)*log10(2)

--
Mats

10. that seems to of worked. thank you.

Just one more thing. how dose the float round to the char? ie up or down or what?

that seems to of worked. thank you.

Just one more thing. how dose the float round to the char? ie up or down or what?
If you just cast a float to char, then it just chops of the decimals, so for example
Code:
`char c = (char)3.9f;`
will result in 3 in c.

--
Mats

12. so how could i round it, so that 3.5 goes to 4 and 3.49 goes to 3?

so how could i round it, so that 3.5 goes to 4 and 3.49 goes to 3?
You would add 0.5 to the result, then 3.5 will become 4.0 which becomes 4. , and 3.49 becomes 3.99 - which rounds down to 3.

--
Mats

14. thank you very much for all of your help

15. Originally Posted by matsp
Like you did, log2(x) == log10(x)*log10(2)

--
Mats
Actually, it is

log2(x) == log10(x) / log10(2)

Todd