Need Help in C programing math

Discussion in 'Programmer's Corner' started by Ralf, Apr 30, 2009.

  1. Ralf

    Thread Starter Active Member

    Mar 16, 2009
    normal maths is 4 X 5
    but i want it in programming language what will it be?
    may i know in C language the multiple is it something like this?

    abc = 4 * 5; (of coz this is not my real program)

    my program for multiple is this:
    analogvoltage1 = result * 1024;
    analogvoltage2 = 5000000/analogvoltage1; //forumal to get my analog voltage
    result1 = 5000000-analogvoltage2;
    result2 = 10000000*analogvoltage2;
    resistor = result2/result1; //forumal to get my register value of R4

    somehow my all result is wrong.
  2. StayatHomeElectronics

    Well-Known Member

    Sep 25, 2008
    Can you show the circuit or problem that you are trying to solve?

    Do you get the correct result in any of the multipication steps that you take? How do you define each of the variables that you use, i.e. analogvoltage1, result, resistor, ..., in the program?
  3. Mark44

    Well-Known Member

    Nov 26, 2007
    To continue where StayAtHomeElectronics left off, what are the types of your variables? I suspect that they might be int.

    If your declaration for result1 and result2 is something like this:
    Code ( (Unknown Language)):
    2. int result1, result2;
    then you will not get the value you probably want in this statement (or in the other statement that involves division):
    Code ( (Unknown Language)):
    2. resistor = result2/result1;
    The reason for this is that there are two kinds of division in C: integer division and floating point division. For example 6/2 is 3, which is what you would expect, but 6/4 is 1and 1/2 is 0, both of which are surprising to C neophytes.