A single line in my code looks like this: - result = 0.57 * ((100 - 1) / (100 + 200));
I have replaced all variables except result with literals, as above, and get this same problem;
result is declared as float (same problem when declared as double) and defined as 0.0
result always stays 0.000 when it should be 0.1881
this calculation works in excel and vb and on a calculator.
Clearly I am missing something important. The app is MFC appwizard dialog. Everything else in this function works except for this one line.
Any ideas?
Thanks
7 1193
Hi,
You may use like this. -
-
result = 0.57 * (double(100 - 1) / double(100 + 200));
-
-
Regards,
M.Sivadhas.
Hi,
You may use like this. -
-
result = 0.57 * (double(100 - 1) / double(100 + 200));
-
-
Regards,
M.Sivadhas.
thank you. Casting is not the problem.
I just built the expression up from scratch to see where the error began and it is in the bracketing. This works: - result = 0.57 * (100 - 1) / (100 + 200);
and this does not work; - result = 0.57 * ((100 - 1) / (100 + 200));
Curious eh?
thank you. Casting is not the problem.
I just built the expression up from scratch to see where the error began and it is in the bracketing. This works: - result = 0.57 * (100 - 1) / (100 + 200);
and this does not work; - result = 0.57 * ((100 - 1) / (100 + 200));
Curious eh?
Yes,
In the first expression, the result will come like this. -
result = 0.57 * (100 - 1) / (100 + 200)
-
result = 0.57 * 99 / 300
-
result = 56.43 / 300
-
result = 56.43 / 300
-
result = 0.1881
-
In the second expression, the result will come like this. -
result = 0.57 * ((100 - 1) / (100 + 200))
-
result = 0.57 * (99 / 300)
-
result = 56.43 / 0
-
result = 0
-
The main reason is due to backet precedence.
Regards,
M.Sivadhas.
result = 0.57 * (99 / 300)
result = 56.43 / 0
This does not make sense to me.
If we assume that bracketing overrides operator prescidence we get this; - result = 0.57 * (99/300)
-
result = 0.57 * (0.33)
-
result = 0.1881
this bracketing works in vb because I copied and pasted the expression and it worked out. vb also uses bracketing to override operator prescidence so something else must be going on with C++ here. I don't know what.
Perhaps the extra set of brackets has caused the (99/300) part to be evaluated as an integer division resulting in 0 thus ending the expression as
thank you. Casting is not the problem.
I just built the expression up from scratch to see where the error began and it is in the bracketing. This works: - result = 0.57 * (100 - 1) / (100 + 200);
and this does not work; - result = 0.57 * ((100 - 1) / (100 + 200));
Curious eh?
in the expression -
result = 0.57 * ((100 - 1) / (100 + 200));
-
the () have the highest precedence therefore the subexpression ((100 - 1) / (100 + 200)) is evaluated first giving 99/300 which (as both operands are int) equals 0
hence result = 0.57 * 0 will be 0
in the expression -
result = 0.57 * (100 - 1) / (100 + 200);
-
* and / have the same precedence and associate from left to right so
(1) (100 – 1) is evaluated giving 99 (an int)
(2) 0.57 * 99 is evaluated – as 0.57 is a double 99 is converted to a double, therefore result is 56.43
(3) (100 + 200) is evaluated giving 300 an int
(4) 56.43 / 300 is evaluated – as 56.43 is a double 300 is converted to a double, therefore result is 0.1881
Excellent thanks.
So I see I can get the original to work by changing this; - result = 0.57 * ((100 - 1) / (100 + 200));
to this; - result = 0.57 * ((100 - 1) / (100 + 200 * 1.0));
and it works. or I could just follow the advice given above with the double cast
I did try out this one; - result = 0.57 * double((100 - 1) / (100 + 200));
which fooled me into thinking that it was not a casting issue
Thanks again both of you.
yes, in -
double result = 0.57 * double((100 - 1) / (100 + 200));
-
the ((100 - 1) / (100 + 200)) is still evaluated as an int giving 0 and then converted to a double
you need to cast one of the ints to a double then evaluate, e.g. -
result = 0.57 * ( double(100 - 1) / (100 + 200));
-
as soon as you have a double the rest of the expression will be converted to double
Sign in to post your reply or Sign up for a free account.
Similar topics
by: Sugapablo |
last post by:
My brain is frozen on a convenient way to figure out if today is the
1st, 2nd, 3rd, 4th, or last Thursday of the month. Basically I need
something that will figure this out for any given day for...
|
by: Wugi |
last post by:
I'm trying to find an equivalent of key-event trapping which is easy in
QBasic. Several of my programs build up a geometric figure with two
parameter line families, eg with two nested for..next...
|
by: Tom |
last post by:
How can you figure out exactly where in the classpath a class is
located ?
For example, assume that my computer has a class
"com.MyCompany.MyClass" that is physically located in the jar-file...
|
by: Nalaka |
last post by:
Hi,
I created a sinple web service that returns a dataSet.
Then I created a client program that uses this web service (that returns the
Dataset).
My question is, how did the client figure...
|
by: Brian Blais |
last post by:
Hello,
I have an odd kind of Heisenbug in what looks like a pretty simple program. The
program is a progress bar code I got at the Python Cookbook:
...
|
by: Rich |
last post by:
Hello,
I have created icons before with icon editor that comes with VS2003 without
any issues. Today I created another icon for my VB.Net app. But this .ico
file is not displaying the figure...
|
by: Rich |
last post by:
Hello,
When I create an icon in VB2005 icon editor - 32x32, the figure I draw in
the editor is not displaying on the icon file in the solution explorere, in
windows explorer, on the exe, or on...
|
by: Leonnhy |
last post by:
HI, I'm a beginner in using MS Access. What's the code for the bring forward figure in current sales report I create as follows:-
Once I click on the sales report, it will show me the date range...
|
by: Frank |
last post by:
Hi,
I use rpy to plot functions and have the following problem. When I
execute the following code line by line (start python and then execute
line by line) the resulting figure looks as it...
|
by: dmitrey |
last post by:
Hi all,
here is a question already mentioned below, and I'm also interested in
that one very much.
unfortunatly, I can't write anything to matplotlib mailing lists
because I constantly get server...
|
by: Charles Arthur |
last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
|
by: BarryA |
last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
|
by: nemocccc |
last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
|
by: marktang |
last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
|
by: Oralloy |
last post by:
Hello folks,
I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>".
The problem is that using the GNU compilers,...
|
by: Hystou |
last post by:
Overview:
Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
|
by: tracyyun |
last post by:
Dear forum friends,
With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
|
by: agi2029 |
last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
|
by: isladogs |
last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM).
In this session, we are pleased to welcome a new...
| |