Thread: addition, multiplication errors??

  1. #1
    Registered User
    Join Date
    Jul 2003
    Posts
    19

    Exclamation addition, multiplication errors??

    I am currently experiencing some difficulty when it comes to simple athrimitics in C. I am using variables that are double, so should be able to handle decimal, multiplying them and/or dividing them by another variable, also a double, and when I go to display the result using for example
    Code:
    printf("%d", bla)
    I get a return of 0 instead of the number I am suppose to get. This happens is several places in my program and I can't figure out why, in some of the arithmitics I am using hex. values (ex. 0xA) but I don't see why that should make a difference.

    If you can offer me any advice it would be greatly appreciated

    Thanks

  2. #2
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Use %f for floating point numbers. %d is for integers.

    Quzah.
    Hope is the first step on the road to disappointment.

  3. #3
    Registered User
    Join Date
    Jul 2003
    Posts
    19
    Ok that is well and good, and I can't believe I didn't think of that, but I'm still stuck with one problem. I am receiving data from an data acquisition board, which returns the data to me in hex. I then need to do some calculation to that value, which I am doing in hex. then show it in decimal. So far when I do my calculations all I do get is 0 as an answer. Below is a segment of the code being used
    Code:
    finalResult = (datavalue/calcValue)*calcValue1;
    fprintf(outOpen,"    %f",finalResult);
    where datavalue is the value recieved from the board in hex. calcValue is 0x1000 and calcValue1 is 0x14.

    When I get a value like 2 from the board I should still be recieving a final value print out of something like 0.002 but I am getting 0. Same with bigger numbers like 153 as a returned value from the board.

    If you can give me any advice, once again it would be greatly apprieciated

    Thanks

  4. #4
    Been here, done that.
    Join Date
    May 2003
    Posts
    1,164
    You're doing all integer math, which will truncate your division to 0. Cast the values to float on the right side of your equal
    Definition: Politics -- Latin, from
    poly meaning many and
    tics meaning blood sucking parasites
    -- Tom Smothers

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. global namespace errors
    By stubaan in forum C++ Programming
    Replies: 9
    Last Post: 04-02-2008, 03:11 PM
  2. Help me find a multiplication toy.
    By QuestionC in forum A Brief History of Cprogramming.com
    Replies: 5
    Last Post: 08-24-2007, 12:27 PM
  3. Addition vs. Multiplication
    By Unregistered in forum Game Programming
    Replies: 11
    Last Post: 07-10-2002, 11:24 AM
  4. executing errors
    By s0ul2squeeze in forum C++ Programming
    Replies: 3
    Last Post: 03-26-2002, 01:43 PM