I'm working on a class for working with fractions and one of the public
functions is to return the decimal value of the fraction which is
simple enough except for when the denominator is 0. I don't want to
return 0.0 because thats not what it is, I would rather return NaN or
INF but I can't figure out how to do that. Does anyone know the
expression for either of these in doubles? Or even the bit pattern
that represents INF and/or NaN? I can't seem to find it posted
anywhere.
double Fraction::Decimal() const{
if (IsValid())
return double(n) / d;
else
return ???;
}
//n is numerator, d is denominator, of course. ??? is the expression I
need to figure out. 6 33569 Ro********@gmail.com wrote: I would rather return NaN or INF but I can't figure out how to do that.
They mean very different things. But unless you're talking about
0.0/0.0. the correct value is infinity, not NaN.
Does anyone know the expression for either of these in doubles?
numeric_limits<double>::infinity() and numeric_limits<double>::quite_NaN().
--
Pete Becker
Dinkumware, Ltd. ( http://www.dinkumware.com)
Pete Becker wrote: numeric_limits<double>::quite_NaN().
Of course, that should be numeric_limits<double>::quiet_NaN().
--
Pete Becker
Dinkumware, Ltd. ( http://www.dinkumware.com)
I'm having some troubles getting that to compile, here's what i've got
now:
double Fraction::Decimal() const{
if (d != 0)
//return decimal equivelent
return double(n) / d;
else if (n != 0)
//return INF
return numeric_limits<double>::infinity();
else
//return NaN
return numeric_limits<double>::quiet_NaN();
}
The errors I am getting are:
error C2039: 'infinity' : is not a member of 'operator``global
namespace'''
error C2039: 'quiet_NaN' : is not a member of 'operator``global
namespace'''
error C2062: type 'double' unexpected
error C2062: type 'double' unexpected
error C2065: 'numeric_limits' : undeclared identifier
The first two are on the lines you would expect, type double errors are
on lines 7 and 10 (from code above) and I find it odd that the last
error only appears once and not twice, it points to line 7.
I'm guessing I slaughtered the code pretty bad, I've never messed
around with calling some barely heard-of function in a standard library
somewhere. I would have guessed it would be included in iostream
somewhere, maybe not? Thanks for the help in advance, would be nice to
know how to get this one working.
<Ro********@gmail.com> wrote in message
news:11**********************@o13g2000cwo.googlegr oups.com... I'm having some troubles getting that to compile, here's what i've got now:
double Fraction::Decimal() const{ if (d != 0) //return decimal equivelent return double(n) / d; else if (n != 0) //return INF return numeric_limits<double>::infinity(); else //return NaN return numeric_limits<double>::quiet_NaN(); }
The errors I am getting are: error C2039: 'infinity' : is not a member of 'operator``global namespace''' error C2039: 'quiet_NaN' : is not a member of 'operator``global namespace''' error C2062: type 'double' unexpected error C2062: type 'double' unexpected error C2065: 'numeric_limits' : undeclared identifier
The error messages are not very helpful, but are probably due to the fact
that <limits> is not included. This should work:
#include <limits>
using std::numeric_limits;
double Decimal()
{
return numeric_limits<double>::infinity();
}
int main()
{
Decimal();
}
Ali Ro********@gmail.com wrote: I would have guessed it would be included in iostream somewhere, maybe not?
Don't guess. Look it up. That's what the documentation is for.
--
Pete Becker
Dinkumware, Ltd. ( http://www.dinkumware.com)
<Ro********@gmail.com> wrote in message
news:11*********************@g49g2000cwa.googlegro ups.com... I'm working on a class for working with fractions and one of the public functions is to return the decimal value of the fraction which is simple enough except for when the denominator is 0. I don't want to return 0.0 because thats not what it is, I would rather return NaN or INF but I can't figure out how to do that. Does anyone know the expression for either of these in doubles? Or even the bit pattern that represents INF and/or NaN? I can't seem to find it posted anywhere.
If you're using Digital Mars C/C++ or any compiler with a C99 conformant
math.h header file:
#include <math.h>
INFINITY for infinity
NAN for nan
Not all floating point implementations support the notion of INFINITY or
NAN.
-Walter www.digitalmars.com C, C++, D programming language compilers This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
by: Ivan Leo Puoti |
last post by:
How can I get a function to return the .xxx part of a floating point number?
Ivan.
|
by: Praveen |
last post by:
How to get rid of this error
|
by: sharmaharish |
last post by:
I need a conversion function that converts values from string to a
particular type. For this I have a template function that looks like
this ...
template<class T>
T value(const string& s)
{...
|
by: SMichal |
last post by:
Hi, how can I parse string "? 20.000" to double ?
|
by: Ole Nielsby |
last post by:
First, sorry if this is off-topic, not strictly being a C++ issue.
I could not find a ng on numerics or serialization and I figure
this ng is the closest I can get.
Now the question:
I want...
|
by: garyusenet |
last post by:
I'm trying to investigate the maximum size of different variable types.
I'm using INT as my starting variable for exploration. I know that the
maximum number that the int variable can take is:...
|
by: HarisHohkl |
last post by:
Hi, I've this function in a class to update the total value.but when i try to remove the these row highlight in Bold it crash, what should i do????
void display_total_value()
{
double...
|
by: mrooney |
last post by:
Hi,
I am having trouble getting Access to recognize two values as being equal to one another. Here's the situation (looks long but I am just trying to be uber-thorough):
I have a large table...
|
by: blazzer |
last post by:
Hi,
I need help. I just wondering whether i can set the double value using random class..i even use if statement to set it..but nothing..i wanna set between 0.5 n 0.7 value..
double planetMass...
|
by: Richard Harnden |
last post by:
Hi,
I've got a list of longs which I need to fix.
Basically the numbers come from an unsigned long, which will wrap back
to zero.
I need them unwrapped.
Also, my data will come from a...
|
by: DolphinDB |
last post by:
The formulas of 101 quantitative trading alphas used by WorldQuant were presented in the paper 101 Formulaic Alphas. However, some formulas are complex, leading to challenges in calculation.
Take...
|
by: DolphinDB |
last post by:
Tired of spending countless mintues downsampling your data? Look no further!
In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
|
by: isladogs |
last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM).
In this month's session, we are pleased to welcome back...
|
by: Vimpel783 |
last post by:
Hello!
Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
|
by: ArrayDB |
last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
|
by: CloudSolutions |
last post by:
Introduction:
For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
|
by: Defcon1945 |
last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
|
by: Shællîpôpï 09 |
last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
|
by: af34tf |
last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
| |