473,322 Members | 1,610 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,322 software developers and data experts.

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.4224999999999
b= 1678.0000000000000

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.78422546364719
b= 1678.00000000000000
a*b = 335237.930328

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

i am not to find the solution.
May 21 '10 #1
3 2187
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.78422546364719.

But still i want to avoid the problem of casting float to double.
May 21 '10 #2
ThatThatGuy
449 Expert 256MB
@karmjit435
Why are you casting the float value to double when you already have a float data type in C#
May 21 '10 #3
Frinavale
9,735 Expert Mod 8TB
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
May 31 '10 #4

Sign in to post your reply or Sign up for a free account.

Similar topics

1
by: Muthiah Samy | last post by:
Hi All, I am trying to call COM Component in .NET using PInvoke. I have rewritten custom IDL file in a managed language(C#). When I try to invoke one of the COM method from .NET, I am getting...
4
by: MFRASER | last post by:
How can I compare two double values and return the greater of the two values? example double doublea = 1.0; double doubleb = 2.0 double retVal = ?compare doublea and doubleb
7
by: Anne | last post by:
hie there, i want to be able to pass multiple parameters to another page. currently, i am able to do so, but somehow i feel it is not the correct way to do it. below is part of what i have so far....
0
by: Peter Stojkovic | last post by:
I am using VB.2005 When I stream a class of several double values into a XML-string, than the values are very long : for example 3.00000000001 or 12.4324634547 Is there any possibility, to limit...
19
by: nickyeng | last post by:
i have a question from my paper, which i dont have the correct answer, so here i asking anyone who can help, provide me the appropriate answer. ----------------------------------- Write a class...
3
by: ishwarbg | last post by:
Hi Everyone, I have a .Net Application, through which I am invoking a function from a legacy DLL developed in C++. My structure in C# contains some data of type double which I need to pass to to...
5
by: ahsuman | last post by:
Hai , This is a bit weird for me.It may be that i got it wrong, but i request you to respond please. All i need to do is add two double values Dim a as double =324.0 Dim b as double=0.1...
2
by: runway27 | last post by:
i am presently passing the value of a variable to a php file using GET after data has been inserted to the database. ex= $firstname = $_POST; if(!$sqlconnection) { echo "error message"; }
5
by: jmartmem | last post by:
Greetings, I have built an Update Record Form in an ASP page. This form contains a number of fields, such as text boxes and menus, to name a few. Upon clicking the 'submit' button, I want the...
4
by: Sagaert Johan | last post by:
Hi I am struggling with pinvoke I have dll a function that is declared as : sf_command (IntPtr sndfile,int command, IntPtr data, int datasize); i need to pass a pointer to a double in the...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.