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

Testing (in)equality against a const float

I've been looking through the FAQ and Googling previous threads on
c.l.c++ but I haven't seen this exact situation addressed. I
initialize variables of type float to a known invalid value (of type
const float), and at a later time I want to see if these variables are
still undefined:

#include <iostream>
int main()
{
const float undefined(-999); // valid only as an initial value

float f1(undefined);
if (f1 == undefined)
{
std::cout << "f1 has not been defined\n";
}

float f2(undefined);
f2 = 5;
if (f2 != undefined)
{
std::cout << "f2 has been defined\n";
}
return 0;
}

Expected output:

f1 has not been defined
f2 has been defined

From what I've read, I think (hope) that floating point comparisons
are okay in this case because I'm using the same type for each
variable in the comparisons and no arithmetic is involved.
Jul 22 '05 #1
2 1861

"Michael Klatt" <md*****@ou.edu> wrote in message
news:2c**************************@posting.google.c om...
I've been looking through the FAQ and Googling previous threads on
c.l.c++ but I haven't seen this exact situation addressed. I
initialize variables of type float to a known invalid value (of type
const float), and at a later time I want to see if these variables are
still undefined:

#include <iostream>
int main()
{
const float undefined(-999); // valid only as an initial value

float f1(undefined);
if (f1 == undefined)
{
std::cout << "f1 has not been defined\n";
}

float f2(undefined);
f2 = 5;
if (f2 != undefined)
{
std::cout << "f2 has been defined\n";
}
return 0;
}

Expected output:

f1 has not been defined
f2 has been defined

From what I've read, I think (hope) that floating point comparisons
are okay in this case because I'm using the same type for each
variable
The issue isn't the types, but the values.
in the comparisons and no arithmetic is involved.


Some value other than 'undefined' might evaluate 'close enough'
for == to return true. But I think this would only be the case
when 'undefined' is not an integral value. If I'm wrong about this,
I'm sure I'll be corrected.

-Mike
Jul 22 '05 #2
Mike Wahler wrote:

Some value other than 'undefined' might evaluate 'close enough'
for == to return true. But I think this would only be the case
when 'undefined' is not an integral value. If I'm wrong about this,
I'm sure I'll be corrected.


Integral values with absolute value < 2^bits, where 'bits' is the number
of bits in the mantissa, can be represented exactly in binary floating
point types.

--

Pete Becker
Dinkumware, Ltd. (http://www.dinkumware.com)
Jul 22 '05 #3

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

Similar topics

2
by: Tristan | last post by:
Hi I'm trying to test if two Pen objects are equal as shown below Pen p1 = new Pen(Color.Black) Pen p2 = new Pen(Color.Black) if (p1.Equals(p2) Console.WriteLine("These pens equal")
12
by: Christof Krueger | last post by:
Hello, I'm quite new to C++ so maybe there's something I miss. I write a simple board game. It has a board class. This class has a method that returns the count of pieces a player has on the...
3
by: shaun | last post by:
I have a function which does not modify a private data member which is a map, but simply returns one value. I'd like to make that member function const, like so: int MapTest:: getValueAt(const...
2
by: John Baker | last post by:
Hi: I have a trivial but frustrating question. I have a record and have set up a query against it, but I cant get it to work! I wish to select the record IF a specific date field is blank. I...
16
by: Gerald Lafreniere | last post by:
{ float F=123.456000; F*=1000; // Actually I used a for loop F*=10 three times. printf("%f\n", F); } This will produce something like 123456.00XXXX, where XXXX are garbage digits. Why...
2
by: Tristan | last post by:
Hi, I'm trying to test if two Pen objects are equal as shown below: Pen p1 = new Pen(Color.Black); Pen p2 = new Pen(Color.Black); if (p1.Equals(p2)) { Console.WriteLine("These pens...
10
by: dwaach | last post by:
Hi, I am trying to compile the following program, #include <iostream> using namespace std; typedef char* CHAR; typedef const CHAR CCHAR;
1
by: tom | last post by:
Hi! I'm wondering whether there's an easy way to remove unnecessary leading zeros from my floating point number. realS = float(-1.25e-5) imgS = float(-7.6e4) print complex(realS, imgS) I...
5
by: Selvam | last post by:
Hi All, I am getting exponent value when doing float arithmetic in C++. Instead of that I need accurate value. float amount = 0.0f; float x = 0.99999976f; amount= 1.0f - x; I am getting...
0
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...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
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...
0
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...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
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...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
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....
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 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 former...

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.