LabWindows/CVI

cancel
Showing results for 
Search instead for 
Did you mean: 

Can not display a double variable

Solved!
Go to solution

I am having problems with formating a double variable as follows:

 

double MEAS;

 

FmtOut ("DMM MEASURMENT = %d\n",MEAS);

 

I tried the scan() but am clueless on how to solve this problem. I am just trying to sending the data to a dos shell window to view.

0 Kudos
Message 1 of 8
(4,344 Views)
Solution
Accepted by erdc3

%d is the formatting code for integers. Try with %f with optional parameter for precision (e.g. %.2f).



Proud to use LW/CVI from 3.1 on.

My contributions to the Developer Community
________________________________________
If I have helped you, why not giving me a kudos?
Message 2 of 8
(4,340 Views)

With the code below I get output of "whole = 1.256" but there is no output at all for the FmtOut ("fix 2 = %1.2f\n", MEASE3); line of code.

 

    double MEASE3;
     MEASE3 = 1.256;
    FmtOut ("whole = %f\n", MEASE3);
    FmtOut ("fix 2 = %1.2f\n", MEASE3);

 

P.S. thanks for your reply

0 Kudos
Message 3 of 8
(4,309 Views)

I am sorry: I give you the formatting code for sprintf function, which is similar but not the same as the one required by FmtOut () function of CVI. There is a very good explanation of this and similar functions with lots of examples in the online help: here you can see the reference page on this function, take a look also to the examples page.

However, I cannot understand what you are aiming to with "%1.2f" formatting code: keep in mind that in standard C the integer part of the formatting code is total field width, while the fractional part is the precision (=digits after decimal point).

Anyway, in FmtOut codes are different and perhaps more clear, so supposing you want one digit before and 2 after the decimal point the corresponding formatting code should be "%f[w4p2]". Nevertheless, when specifying the field width you are subject to get some asterisk in case the number cannot be represented with such width: for example, 10.25 will be formatted in "*10.25" with the preceding code. I normally omit the field width unless I need to format a string with an exact lenght (which does not seem to be your case).



Proud to use LW/CVI from 3.1 on.

My contributions to the Developer Community
________________________________________
If I have helped you, why not giving me a kudos?
Message 4 of 8
(4,304 Views)

Thanks RobertoBozzolo that did the trick. Why I needed to format is to check two different measurements and see if they are equal to one another. One measurement reports back just with a tenth of a volt resolution (MEASE2), were the other measurement (MEAS) is up to 6 places of precision. So the FmtOut ("VDC = %f[ww3p1]", MEAS) prints out a 1 digit precision, but how do I do a compare that number as a single precision to the other?

 

while {MEAS != MEAS2) {

// take another reading of MEAS and MEAS2

}

0 Kudos
Message 5 of 8
(4,277 Views)

Bear in mind that comparing floating point numbers is different from comparing integers due to round off errors and limited precision in representing the value, see the discussion here

0 Kudos
Message 6 of 8
(4,268 Views)

When dealing with floating point numbers you must not only consider the different resolution but also consider the inability to represent exaclty all values due to the way such numbers are handled in memory (this argument has been discussed often: search for IEEE754 on the forum or look into this Wikipedia page). For this reason it is not possible to take a simple equality comparison between doubles without considering all these effects, and CVI comes with a function to accomplish it in the Programmer's Toolbox: FP_Compare.

 

Now, while this is not exactly your problem, in my opinion the concept that function is based on can be applied to your problem too: that is, determine the uncertainty due to the different resolution on the two measures and consider equal two numbers whose difference lie within that uncertainty.

 

 

** Edit ** Wolfgang was more concise and therefore faster than me! Smiley Wink Smiley Very Happy



Proud to use LW/CVI from 3.1 on.

My contributions to the Developer Community
________________________________________
If I have helped you, why not giving me a kudos?
0 Kudos
Message 7 of 8
(4,264 Views)

Thank you ever so much for your inputs and links. I now know much better on how the double (whammy) problems my crop up on you.

0 Kudos
Message 8 of 8
(4,246 Views)