473,651 Members | 2,518 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

what mean this " return x < 0 ? 2 - y : y;" in the end of function

1 New Member
Hi
please help me to depict what does mean this " return x < 0 ? 2 - y : y;" in the end of function.

the function is as follows:

Expand|Select|Wrap|Line Numbers
  1. double erfc(double x)
  2. {
  3.     double t, u, y;
  4.  
  5.     t = 3.97886080735226 / (fabs(x) + 3.97886080735226);
  6.     u = t - 0.5;
  7.     y = (((((((((0.00127109764952614092 * u + 1.19314022838340944e-4) * u - 
  8.         0.003963850973605135) * u - 8.70779635317295828e-4) * u + 
  9.         0.00773672528313526668) * u + 0.00383335126264887303) * u - 
  10.         0.0127223813782122755) * u - 0.0133823644533460069) * u + 
  11.         0.0161315329733252248) * u + 0.0390976845588484035) * u + 
  12.         0.00249367200053503304;
  13.     y = ((((((((((((y * u - 0.0838864557023001992) * u - 
  14.         0.119463959964325415) * u + 0.0166207924969367356) * u + 
  15.         0.357524274449531043) * u + 0.805276408752910567) * u + 
  16.         1.18902982909273333) * u + 1.37040217682338167) * u + 
  17.         1.31314653831023098) * u + 1.07925515155856677) * u + 
  18.         0.774368199119538609) * u + 0.490165080585318424) * u + 
  19.         0.275374741597376782) * t * exp(-x * x);
  20.     return x < 0 ? 2 - y : y;
  21. }
Thanks
Feb 13 '13 #1
2 4664
donbock
2,426 Recognized Expert Top Contributor
Expand|Select|Wrap|Line Numbers
  1. return x < 0 ? 2-y : y;
is equivalent to
Expand|Select|Wrap|Line Numbers
  1. if (x < 0)
  2.    a = 2-y;
  3. else
  4.    a = y;
  5. return a;
x < 0 ? 2-y ? y is called a conditional expression.
The ? operator is called the ternary operator.

Personally, I don't find much use for the ternary operator in the code that I write.
Feb 13 '13 #2
weaknessforcats
9,208 Recognized Expert Moderator Expert
The idea behind the ternary operator comes from the old days when computers had very little memory. Generally, the less code you write, the less instructions are generated by the compiler. The ternary operator was an attempt to generate the least amout of code in the executable.

And ditto, I don't use it either.
Feb 13 '13 #3

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

Similar topics

32
8834
by: Mike Machuidel | last post by:
Hi, I'm a game developer programming mostly in C and ASM for about 7 years. Today at work a colleague (a C++ programmer) yelled at me I'm a bad C programmer because I use "return(0);" instead of "return 0;". He explained that "return" is not a function but a stament, like I didn't know already. The other colleagues also argreed with him :(. Can someone please explain what's so wrong about using "return" with
72
4187
by: Paminu | last post by:
In math this expression: (a < b) && (b < c) would be described as: a < b < c But why is it that in C these two expressions evaluate to something different for the same values of a, b and c?
4
1497
by: Alex Sedow | last post by:
Method invocation will consist of the following steps: 1. Member lookup (14.3) - evaluate method group set (Base.f() and Derived.f()) .Standart say: "The compile-time processing of a method invocation of the form M(A), where M is a method group and A is an optional argument-list, consists of the following steps:  The set of candidate methods for the method invocation is constructed. Starting with the set of methods associated with M,...
6
17098
by: murgan | last post by:
Hi people, i am new to this group,this is my first query, friends i want to know the difference between "function pointer" and "pointer to a function" in c lang, so friends please send the answers as early as possible, thanking u all vijay
2
1847
by: Manuel | last post by:
In example, here: ------------------------- namespace Gooey Button::Button(Window* aParent, const std::string& aText, BasicButtonGroup* aGroup) : BasicButton(aParent, aText, aGroup) { } Window& Button::draw()
40
3120
by: Mark P | last post by:
I'm implementing an algorithm and the computational flow is a somewhat deep. That is, fcn A makes many calls to fcn B which makes many calls to fcn C, and so on. The return value of the outermost fcn is a boolean and there are certain places within the inner functions where it may become apparent that the return value is false. In this case I'd like to halt the computation immediately and return false. My current approach is to have...
32
2194
by: Axel Bock | last post by:
Hi all, I am trying to get my head around what happens if I return a class object from a function. It seems C++ (MinGW) does not invoke the copy constructor if I do something like this: SomeObj foo() { SomeObj X;
4
1312
by: yaru22 | last post by:
In one of the examples in the book I'm reading, it says: def __init__(self): ... ... ... return It has nothing after "return". I expected it to have some number like 0 or 1.
36
2580
by: Pat | last post by:
Hi, I've run into a strange problem, but one that seems like it might be fairly common. I have a single base class, from which several other classes are derived. To keep the example simple, the base class is "Animal" and the derived classes are "Cat," "Dog," and "Horse." The base class has a pure virtual method:
13
7795
by: Steve | last post by:
On page 392 of "Javascript the definitive guide" a function is called like this:- <form action="processform.cgi" onsubmit="return validateForm();"> Why, in this instance, is the return statement used in calling the function validateForm rather than being included inside the function? Thanks in advance,
0
8275
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8795
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
8695
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
8460
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8576
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
6157
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
4281
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
1906
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
2
1585
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.