Maths in C

This is a discussion on Maths in C within the C Programming forums, part of the General Programming Boards category; I am trying to perform 2 math equations in C and send the result as a char x = 64*(pow(2,(conversionH/256))-1); ...

  1. #1
    Registered User
    Join Date
    May 2008
    Posts
    11

    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.
    Last edited by username101; 05-09-2008 at 08:36 AM.

  2. #2
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    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
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  3. #3
    Registered User
    Join Date
    May 2008
    Posts
    11
    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. #4
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    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
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  5. #5
    Jack of many languages Dino's Avatar
    Join Date
    Nov 2007
    Location
    Katy, Texas
    Posts
    2,309
    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
    Last edited by Dino; 05-09-2008 at 09:09 AM. Reason: corrected incorrect statement
    Mac and Windows cross platform programmer. Ruby lover.

    Quote of the Day
    12/20: Mario F.:I never was, am not, and never will be, one to shut up in the face of something I think is fundamentally wrong.

    Amen brother!

  6. #6
    Registered User
    Join Date
    May 2008
    Posts
    11
    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. #7
    Jack of many languages Dino's Avatar
    Join Date
    Nov 2007
    Location
    Katy, Texas
    Posts
    2,309
    Cast all your numeric constants, like matsp said, to float, ie, 256.0, not 256.
    Mac and Windows cross platform programmer. Ruby lover.

    Quote of the Day
    12/20: Mario F.:I never was, am not, and never will be, one to shut up in the face of something I think is fundamentally wrong.

    Amen brother!

  8. #8
    Registered User
    Join Date
    May 2008
    Posts
    11
    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
    ???

  9. #9
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by username101 View Post
    hello Todd Burch,

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

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  10. #10
    Registered User
    Join Date
    May 2008
    Posts
    11
    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?

  11. #11
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by username101 View Post
    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
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  12. #12
    Registered User
    Join Date
    May 2008
    Posts
    11
    so how could i round it, so that 3.5 goes to 4 and 3.49 goes to 3?

  13. #13
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by username101 View Post
    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
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  14. #14
    Registered User
    Join Date
    May 2008
    Posts
    11
    thank you very much for all of your help

  15. #15
    Jack of many languages Dino's Avatar
    Join Date
    Nov 2007
    Location
    Katy, Texas
    Posts
    2,309
    Quote Originally Posted by matsp View Post
    Like you did, log2(x) == log10(x)*log10(2)

    --
    Mats
    Actually, it is

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

    Todd
    Mac and Windows cross platform programmer. Ruby lover.

    Quote of the Day
    12/20: Mario F.:I never was, am not, and never will be, one to shut up in the face of something I think is fundamentally wrong.

    Amen brother!

Page 1 of 2 12 LastLast
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Maths
    By AcerN30 in forum A Brief History of Cprogramming.com
    Replies: 7
    Last Post: 05-03-2008, 02:13 PM
  2. maths???
    By nerdyneo in forum C++ Programming
    Replies: 4
    Last Post: 11-09-2003, 01:04 PM
  3. Is maths REALLY required for programming?
    By FloatingPoint in forum A Brief History of Cprogramming.com
    Replies: 29
    Last Post: 07-12-2003, 02:18 PM
  4. maths in a program
    By anthonye in forum C Programming
    Replies: 2
    Last Post: 05-24-2002, 10:08 AM
  5. More Maths :(
    By (TNT) in forum A Brief History of Cprogramming.com
    Replies: 3
    Last Post: 01-20-2002, 10:39 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21