473,386 Members | 1,924 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,386 software developers and data experts.

Calculations inline with conditionals

I'm using Visual C++.Net 2003, and I have noticed a little quirkiness
when using a conditional based on the comparison of two calculations
that are performed inline with the conditional.

For example, if I run this code:

double angle1;
double angle2;

angle1 = 45;
angle2 = 45;

if( tan( angle1 ) < tan( angle2 ) )
cout << "Angle 1";
else
cout << "Angle 2";

I would expect the conditional to return false and the output to be
"Angle 2". What actually happens, however, is that the output is
"Angle 1".

If, instead, I write the code so as to perform the calculation prior to
the conditional like so:

double angle1;
double angle2;
double temp1;
double temp2;

angle1 = 45;
angle2 = 45;

temp1 = tan( angle1 );
temp2 = tan( angle2 );

if( temp1 < temp2 )
cout << "Angle 1";
else
cout << "Angle 2";

the code runs as I would expect and the output is "Angle 2".

Can somebody explain what I am missing? What is the difference in the
way the two snippets work that would cause what I believe to be two
functionally identical pieces of code to operate differently.

Oct 28 '05 #1
3 1507
jdwyer3 wrote:
I'm using Visual C++.Net 2003 [snip] Can somebody explain what I am missing?


Look up the /Op (Improve Float consistency) option
in your compilers documentation.
--
Karl Heinz Buchegger
kb******@gascad.at
Oct 28 '05 #2
jdwyer3 wrote:
I'm using Visual C++.Net 2003, and I have noticed a little quirkiness
when using a conditional based on the comparison of two calculations
that are performed inline with the conditional.


http://www.parashift.com/c++-faq-lit...html#faq-29.17

--John Ratliff
Oct 28 '05 #3
John Ratliff wrote:
jdwyer3 wrote:
I'm using Visual C++.Net 2003, and I have noticed a little quirkiness
when using a conditional based on the comparison of two calculations
that are performed inline with the conditional.


http://www.parashift.com/c++-faq-lit...html#faq-29.17


If I was running into the problem of the OP, reading that FAQ item would not
give me piece of mind. Have a closer look at the code under consideration:

double angle1;
double angle2;

angle1 = 45;
angle2 = 45;

if( tan( angle1 ) < tan( angle2 ) )
cout << "Angle 1";
else
cout << "Angle 2";

If the program prints Angle 1, it takes license to evaluate tan(45)
inconsistently. Note that after initialization angle1 == angle2 is
guaranteed by the standard. Yet the OP finds tan(angle1) != tan(angle2).

Now, look at the code from the FAQ:

double x = 1.0 / 10.0;
double y = x * 10.0;
if (y != 1.0)
std::cout << "surprise: " << y << " != 1\n";

Well, it is unreasonable to expect to different floating point calculations
to yield identical results just because they happen to be mathematically
equivalent -- unreasonable at least once one considers how real numbers are
represented in the computer. However, it is reasonable to expect a function
call to tan() to return identical values for identical arguments: this is
comparing the results from performing the same calculation two times.

I agree that the standard does no guarantee the natural behavior of math
functions (in fact it does not guarantee anything meaningful). Also, there
are good reasons why one might not want to make any stronger guarantees
here: The compiler can inline the function call and do optimizations that
take into account the callers context. For floating point arithmetic, we
might want to allow for those optimizations to slightly change the results.
However, that issue is not addressed in the FAQ. Maybe one should add this
case to the list of warnings:

Double-check your assumptions, including "obvious" things like how to
compute averages, how to solve quadratic equations, etc., etc. Do not
assume the formulas you learned in High School will work with floating
point numbers!

to include "obvious things like two calls to tan(45.0) will evaluate to
equal results."
Best

Kai-Uwe Bux
Oct 28 '05 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: FHuang | last post by:
Ok, I'm having some trouble with conditionals, for some reason PHP is screwing them up. First off, here is the code: <?php $username = "fred"; $userdata = "./$username.txt"; $fp =...
15
by: Joshua Ginsberg | last post by:
Is there any plan to include inline conditionals in Python? For example: def isNegative(x): return x < 0 ? True : False Thanks! -jag --
4
by: Jon Maz | last post by:
Hi All, I'm doing UrlRewriting based on the model in Scott Mitchell's article "URL Rewriting in ASP.NET"....
2
by: Steven T. Hatton | last post by:
I don't really understand the details of what happens when a compiler inlines a given function. I know that basically it's supposed to cause the function to be placed directly on the stack per...
1
by: Paul Dale | last post by:
Hi All, I know that several of you will probably want to reply "you should write a parser", and I may. For that matter any tips on theory in that direction would be appreciated. However, if...
11
by: lduperval | last post by:
Hi, I`m trying to do date calculations in three types of time zones: local, GMT and specified. The issue I am facing is that I need to be able to specify a date in the proper time zone, and I`m...
3
by: steven | last post by:
Is it possible to combine conditionals to call out data? I have a list of records that include a date. I can call out all dates after the date after tomorrow: IIf(Date()+1)<,...,... And I can...
4
by: Leon Lambert | last post by:
I would appreciate it if someone could help me understand NaN handling with respect to conditionals in IL code. I am playing with a small IL interpreter and having a little problem with it....
5
by: Hul Tytus | last post by:
comp.lang.c c programs & shell conditionals How is a unix shell script made to respond to the value returned by a program compiled from c code? The shell script below is my current effort,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
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
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
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
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

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.