473,397 Members | 2,084 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,397 software developers and data experts.

Can you figure this out?

1,646 1GB
A single line in my code looks like this:

Expand|Select|Wrap|Line Numbers
  1. 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
Dec 4 '06 #1
7 1193
sivadhas2006
142 100+
Hi,

You may use like this.

Expand|Select|Wrap|Line Numbers
  1.  
  2. result = 0.57 * (double(100 - 1) / double(100 + 200));
  3.  
  4.  
Regards,
M.Sivadhas.
Dec 4 '06 #2
willakawill
1,646 1GB
Hi,

You may use like this.

Expand|Select|Wrap|Line Numbers
  1.  
  2. result = 0.57 * (double(100 - 1) / double(100 + 200));
  3.  
  4.  
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:
Expand|Select|Wrap|Line Numbers
  1. result = 0.57 * (100 - 1) / (100 + 200);
and this does not work;
Expand|Select|Wrap|Line Numbers
  1. result = 0.57 * ((100 - 1) / (100 + 200));
Curious eh?
Dec 4 '06 #3
sivadhas2006
142 100+
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:
Expand|Select|Wrap|Line Numbers
  1. result = 0.57 * (100 - 1) / (100 + 200);
and this does not work;
Expand|Select|Wrap|Line Numbers
  1. result = 0.57 * ((100 - 1) / (100 + 200));
Curious eh?
Yes,

In the first expression, the result will come like this.
Expand|Select|Wrap|Line Numbers
  1. result = 0.57 * (100 - 1) / (100 + 200)
  2. result = 0.57 * 99 / 300
  3. result = 56.43 /  300
  4. result = 56.43 /  300
  5. result = 0.1881
  6.  

In the second expression, the result will come like this.
Expand|Select|Wrap|Line Numbers
  1. result = 0.57 * ((100 - 1) / (100 + 200))
  2. result = 0.57 * (99 / 300)
  3. result = 56.43 /  0
  4. result = 0
  5.  
The main reason is due to backet precedence.

Regards,
M.Sivadhas.
Dec 4 '06 #4
willakawill
1,646 1GB
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;
Expand|Select|Wrap|Line Numbers
  1. result = 0.57 * (99/300)
  2. result = 0.57 * (0.33)
  3. 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
Expand|Select|Wrap|Line Numbers
  1. result = 0.57 * (0)
Dec 4 '06 #5
horace1
1,510 Expert 1GB
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:
Expand|Select|Wrap|Line Numbers
  1. result = 0.57 * (100 - 1) / (100 + 200);
and this does not work;
Expand|Select|Wrap|Line Numbers
  1. result = 0.57 * ((100 - 1) / (100 + 200));
Curious eh?
in the expression
Expand|Select|Wrap|Line Numbers
  1. result = 0.57 * ((100 - 1) / (100 + 200));
  2.  
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
Expand|Select|Wrap|Line Numbers
  1. result = 0.57 * (100 - 1) / (100 + 200);
  2.  
* 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
Dec 4 '06 #6
willakawill
1,646 1GB
Excellent thanks.
So I see I can get the original to work by changing this;
Expand|Select|Wrap|Line Numbers
  1. result = 0.57 * ((100 - 1) / (100 + 200));
to this;
Expand|Select|Wrap|Line Numbers
  1. 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;
Expand|Select|Wrap|Line Numbers
  1. 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.
Dec 4 '06 #7
horace1
1,510 Expert 1GB
yes, in
Expand|Select|Wrap|Line Numbers
  1. double result = 0.57 * double((100 - 1) / (100 + 200));
  2.  
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.
Expand|Select|Wrap|Line Numbers
  1. result = 0.57 * ( double(100 - 1) / (100 + 200));
  2.  
as soon as you have a double the rest of the expression will be converted to double
Dec 4 '06 #8

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

Similar topics

2
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...
4
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...
1
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...
7
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...
3
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: ...
1
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...
0
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...
1
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...
0
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...
2
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...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
BarryA
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...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
marktang
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,...
0
Oralloy
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,...
0
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...
0
tracyyun
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...
0
agi2029
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,...
0
isladogs
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...

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.