How to pass double values using PInvoke

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • karmjit435
    New Member
    • Aug 2009
    • 3

    #1

    How to pass double values using PInvoke

    I have a c++ component. It exposes an API which accept two double values:

    say:
    void Multiply(double a, double b);

    if i call this API from a C# code using PInoke and pass the values
    a=199.4225
    b=1678
    a*b = 335237.92955
    andi debug the code and found that in the Autos window the values are actually
    a= 199.42249999999 99
    b= 1678.0000000000 000

    If i call this method from a c++ code and pass the same values of a and b.
    I debug the code and Autos window show s the values:
    a= 199.78422546364 719
    b= 1678.0000000000 0000
    a*b = 335237.930328

    because of these changes i am getting wrong values in furthur calculations.

    i am not to find the solution.
  • karmjit435
    New Member
    • Aug 2009
    • 3

    #2
    I think i have found the problem.

    I am passing a float and assigning the float value to the double.

    float a = 199.784225;
    double d = a;

    and value of d comes out to be = 199.78422546364 719.

    But still i want to avoid the problem of casting float to double.

    Comment

    • ThatThatGuy
      Recognized Expert Contributor
      • Jul 2009
      • 453

      #3
      Originally posted by karmjit435
      I think i have found the problem.

      I am passing a float and assigning the float value to the double.

      float a = 199.784225;
      double d = a;

      and value of d comes out to be = 199.78422546364 719.

      But still i want to avoid the problem of casting float to double.
      Why are you casting the float value to double when you already have a float data type in C#

      Comment

      • Frinavale
        Recognized Expert Expert
        • Oct 2006
        • 9749

        #4
        Double's take up 2 chunks of memory (side by side) to represent a number.

        A Float only takes up 1 chunk of memory.

        Usually a "chunk" of memory is 32-bits, so this means that a double is 64 bits while a float is only 32 bits.

        The more memory that you can use to represent a "real" number (like a float or double) the better. With more bits to use to represent the number you can get better precision (accuracy) and range for the number.

        For more information on floats check out this article on Floating point types, and this dry but detailed article on how floating points work: What Every Computer Scientist Should Know About Floating-Point Arithmetic. Wikipedia actually has an easy to understand article that nicely explains Floating Points too.

        Anyways, if you cast a double into a float you are losing the space (physical bits) required to accurately represent the double number. This means that the number must be rounded to approximately the same value.

        Floats/doubles are always approximate anyways. They are interesting data types and I recommend that you check out this topic in more detail.

        -Frinny

        Comment

        Working...