473,803 Members | 2,038 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

how to express conception infinity in c++

In my program I must have some variables which values are infinity.
It means the variable must be
bigger than any integer. And if we add some to it, its value should
still be infinity.

I try to use the max value of int ,but it will cause overflow if you
add something to it.
I also try to write a class INF,in which I overload operator + and >.
But those variables sometimes
should be integer,i can not make them be the in instances of INF.

Please help me to the resolve problem !

Jun 20 '07 #1
5 9805
westhood wrote:
In my program I must have some variables which values are infinity.
It means the variable must be
bigger than any integer. And if we add some to it, its value should
still be infinity.

I try to use the max value of int ,but it will cause overflow if you
add something to it.
I also try to write a class INF,in which I overload operator + and >.
But those variables sometimes
should be integer,i can not make them be the in instances of INF.

Please help me to the resolve problem !
If you design your INF class, you could still try to provide the
'operator int() const' (so called "conversion function"), but I am
not sure what you're going to return from it. INT_MAX, perhaps?
Which doesn't sound like a good idea...

It seems that you need to design your own wrapper around 'int', and
*extend* it by allowing it to have the value "infinity". Do NOT let
it be convertible _to_ int, only _from_ int. You will have to give
it all the traits of 'int' (overload all the operators you need to
use it with, like + - * / % @= < <= >= == != ^ | & ~). Seems like
lots of work, but it's not really. You only need to overload the
ones you use in your program. Also, make sure that to overload the
binary ones (except the compound assignments) as non-members, it'll
allow you to write

MyInt a(42);
2 + a;

This is a very good exercise how to properly *extend* some other type.

V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
Jun 20 '07 #2
westhood <we*******@gmai l.comwrote:
>In my program I must have some variables which values are infinity.
It means the variable must be
bigger than any integer. And if we add some to it, its value should
still be infinity.

I try to use the max value of int ,but it will cause overflow if you
add something to it.
I also try to write a class INF,in which I overload operator + and >.
But those variables sometimes
should be integer,i can not make them be the in instances of INF.

Please help me to the resolve problem !
I'd suggest a class which contains an integer and a flag. The flag is
set true or false to designate whether the number is infinity or not.
If the flag is true, then your "+" override does nothing and your ">"
override always returns false (I guess, you can make it do whatever
you want of course). If the flag is false, "+" does a normal addition,
">" does a normal comparison.

--
Tim Slattery
Sl********@bls. gov
http://members.cox.net/slatteryt
Jun 20 '07 #3
On Jun 20, 5:34 am, westhood <westho...@gmai l.comwrote:
In my program I must have some variables which values are infinity.
It means the variable must be
bigger than any integer. And if we add some to it, its value should
still be infinity.

I try to use the max value of int ,but it will cause overflow if you
add something to it.
I also try to write a class INF,in which I overload operator + and >.
But those variables sometimes
should be integer,i can not make them be the in instances of INF.

Please help me to the resolve problem !
A potential solution could be to have a class with a int data member
to store the value and a boolean flag that indicates if the value is
infinite. Once the value becomes infinite the overloaded operators +,
- should just return. I dunno if that answers your question.

Jun 20 '07 #4
westhood a ¨¦crit :
In my program I must have some variables which values are infinity.
It means the variable must be
bigger than any integer. And if we add some to it, its value should
still be infinity.

I try to use the max value of int ,but it will cause overflow if you
add something to it.
I also try to write a class INF,in which I overload operator + and >.
But those variables sometimes
should be integer,i can not make them be the in instances of INF.

Please help me to the resolve problem !
What do you mean by "those variables sometimes should be integer" ?

If they should be integer then you can only use integer operation (no
infinity).

If the parameter is required to be an integer in type you can always use
a test to known if an integer represent infinity and act accordingly.

If you can pass a class but sometimes this class should be considered an
integer, perhaps an automatic cast is enough.

Michael
Jun 20 '07 #5
On Wed, 20 Jun 2007 08:55:22 -0400, Tim Slattery wrote:
westhood <we*******@gmai l.comwrote:
>>In my program I must have some variables which values are infinity. It
means the variable must be
bigger than any integer. And if we add some to it, its value should
still be infinity.

I try to use the max value of int ,but it will cause overflow if you add
something to it.
I also try to write a class INF,in which I overload operator + and >.
But those variables sometimes
should be integer,i can not make them be the in instances of INF.

Please help me to the resolve problem !

I'd suggest a class which contains an integer and a flag. The flag is
set true or false to designate whether the number is infinity or not. If
the flag is true, then your "+" override does nothing and your ">"
override always returns false (I guess, you can make it do whatever you
want of course). If the flag is false, "+" does a normal addition, ">"
does a normal comparison.
If you're going to have infinity as a value you'll also have to decide
how to handle infinity - infinity (minus) which is... undefined. So you
may want a tri-state flag and also implement an "undefined value" (handy
for 0/0 too!).

This starts to look like floating point implementations where undefined =
NaN. In fact you might consider seeing how floating point handles these
things. std::numeric_li mits might be a good place to start.

--
Lionel B
Jun 20 '07 #6

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

Similar topics

4
68763
by: Andreas Neudecker | last post by:
Hi. Is there anything like +infinity and -infinity available in Python, and can it be used in comparisons together with int or float numbers? Regards Andreas
108
6472
by: Bryan Olson | last post by:
The Python slice type has one method 'indices', and reportedly: This method takes a single integer argument /length/ and computes information about the extended slice that the slice object would describe if applied to a sequence of length items. It returns a tuple of three integers; respectively these are the /start/ and /stop/ indices and the /step/ or stride length of the slice. Missing or out-of-bounds indices are handled in a manner...
1
6895
by: Amit | last post by:
Hi, I am trying to use INFINITY in a c++ code that I have written. I am using gcc 3.3 on os X 10.3.9 The compiler gives a warning warning: floating constant exceeds range of "float" My code uses the following where this error is given: double c = INFINITY;
2
4375
by: Russell Smith | last post by:
Timestamps support infinity. However if appears dates do not. When timestamps are cast to dates, there is no output. Is this an acceptable option or not? Below are a number of examples showing what I am experiencing. The last own shows how converting timestamps to dates and then ordering doesn't give you the order you want. Maybe you should just order by the timestamp to begin with. However Date does not understand infinity at all.
5
4202
by: Peter Hansen | last post by:
I'm investigating a puzzling problem involving an attempt to generate a constant containing an (IEEE 754) "infinity" value. (I understand that special float values are a "platform-dependent accident" etc...) The issue appears possibly to point to a bug in the Python compiler, with it producing inconsistent results. I'm using "Python 2.4.2 (#67, Sep 28 2005, 12:41:11) on win32". This code sometimes produces a float of 1.0, sometimes...
2
1924
by: Pierre Rouleau | last post by:
Hi all, When using Python 2.4.x on a Win32 box, marshal.loads(marshal.dumps(1e66666)) returns 1.0 instead of infinity as it should and does under Python 2.5 (also running on Win32 ). This problem was reported in another thread here by Peter Hansen http://groups-beta.google.com/group/comp.lang.python/browse_frm/thread/5c2b4b2a88c8df4/f216739705c9304f?lnk=gst&q=simplejson&rnum=5#f216739705c9304f Is this considered an important enough...
12
2574
by: horacius.rex | last post by:
Hi, I have a code that in some part of the program calculates 1/x for a lot of different x's. About 1 of 100 times x is equal to zero, so when I print the result I obtain inf. I wonder if there is a way to detect this "infinity" and convert it to the float zero. I mean something like for i = .... calculate 1/x_i
14
5555
by: Jim Langston | last post by:
The output of the following program is: 1.#INF 1 But: 1.#INF 1.#INF was expected and desired. How can I read a value of infinity from a stream?
13
2324
by: kimiraikkonen | last post by:
Hello, I have an aritmetic calculation like this: First note that: i need a "timer" to get the value for value3. (however removing "timer" didn't differ) Dim value1 As Long Dim value2 As String Dim value3 As String value3 = (value2 * 8) / value1
0
9562
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
10542
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
10309
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...
0
9119
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7600
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
6840
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5496
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5625
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
3
2968
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.