473,734 Members | 2,511 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Arithmetic properties of HUGE_VAL and errno

On my system, the following five expressions are true:

(HUGE_VAL == HUGE_VAL / 2)
(1 / HUGE_VAL == 0)
(sqrt(HUGE_VAL) == HUGE_VAL)
(sqrt(-1) == HUGE_VAL)
(sqrt(-HUGE_VAL) == HUGE_VAL)

and

sqrt(HUGE_VAL) doesn't set errno, but sqrt(-1) and sqrt(-HUGE_VAL) do.

Does that all seem right?

--
pete
Nov 15 '05 #1
18 3432

pete wrote:
On my system, the following five expressions are true:

(HUGE_VAL == HUGE_VAL / 2)
(1 / HUGE_VAL == 0)
(sqrt(HUGE_VAL) == HUGE_VAL)
(sqrt(-1) == HUGE_VAL)
(sqrt(-HUGE_VAL) == HUGE_VAL)

and

sqrt(HUGE_VAL) doesn't set errno, but sqrt(-1) and sqrt(-HUGE_VAL) do.

Does that all seem right?

Your implementation apparently defines HUGE_VAL as a positive infinity,
which is legitimate. All the rest flows pretty obviously from there.

Nov 15 '05 #2
pete wrote:
On my system, the following five expressions are true:

(HUGE_VAL == HUGE_VAL / 2)
(1 / HUGE_VAL == 0)
(sqrt(HUGE_VAL) == HUGE_VAL)
(sqrt(-1) == HUGE_VAL)
(sqrt(-HUGE_VAL) == HUGE_VAL)

and

sqrt(HUGE_VAL) doesn't set errno, but sqrt(-1) and sqrt(-HUGE_VAL) do.

Does that all seem right?


If HUGE_VAL and HUGE_VALF are INFINITY, as they are in my
implementation, that would explain much. Your question has led me to
discover that HUGE_VALL is broken on my implementation (it is an
unnormalized NaN).
Nov 15 '05 #3
Martin Ambuhl wrote:
If HUGE_VAL and HUGE_VALF are INFINITY, as they are in my
implementation, that would explain much. Your question has led me to
discover that HUGE_VALL is broken on my implementation (it is an
unnormalized NaN).


An "unnormaliz ed" NaN? I'm having trouble with
the concept; can you explain in more detail?

--
Eric Sosman
es*****@acm-dot-org.invalid
Nov 15 '05 #4
ro***********@y ahoo.com wrote:

pete wrote:
On my system, the following five expressions are true:

(HUGE_VAL == HUGE_VAL / 2)
(1 / HUGE_VAL == 0)
(sqrt(HUGE_VAL) == HUGE_VAL)
(sqrt(-1) == HUGE_VAL)
(sqrt(-HUGE_VAL) == HUGE_VAL)

and

sqrt(HUGE_VAL) doesn't set errno,
but sqrt(-1) and sqrt(-HUGE_VAL) do.

Does that all seem right?


Your implementation apparently
defines HUGE_VAL as a positive infinity,
which is legitimate. All the rest flows pretty obviously from there.


Thank you. I have more questions.
What other options are there besides positive infinity?
Could sqrt(HUGE_VAL) be undefined?
Is it allowed for sqrt(HUGE_VAL) to set errno to errange
if it returns HUGE_VAL?
Are any of the above 5 expressions
guaranteed to be true by the standard,
or is it merely the case that they are allowed to be true?

Suppose I want to write a 100% portable function for C89 and C99
that does exactly what log2 does. Could it be written this way?:

double l_og2(double x)
{
return log(x) / log(2);
}

The point being, Can I really on any funky values like HUGE_VAL,
which might be returned by log(x),
to not be altered by being divided by log(2)?
Or would this be more portable?:

double l_og2(double x)
{
return x > 0 && DBL_MAX >= x ? log(x) / log(2) : log(x);
}
It seems that most settings of errno to ERANGE are optional,
but for exp, it says that you do get them when the magnitude of x
is too large, not x, but the magnitude of x.
Is setting errno to ERANGE optional or mandatory for exp(-DBL_MAX)?
I think it would be a case of underflow.
On a system like mine, where HUGE_VAL is an infinity,
Is setting errno to ERANGE optional or mandatory for exp(-HUGE_VAL)?

--
pete
Nov 15 '05 #5
pete wrote:
errange


I meant ERANGE.

--
pete
Nov 15 '05 #6
pete wrote:
ro***********@y ahoo.com wrote:

pete wrote:
On my system, the following five expressions are true:

(HUGE_VAL == HUGE_VAL / 2)
(1 / HUGE_VAL == 0)
(sqrt(HUGE_VAL) == HUGE_VAL)
(sqrt(-1) == HUGE_VAL)
(sqrt(-HUGE_VAL) == HUGE_VAL)

and

sqrt(HUGE_VAL) doesn't set errno,
but sqrt(-1) and sqrt(-HUGE_VAL) do.

Does that all seem right?
Your implementation apparently
defines HUGE_VAL as a positive infinity,
which is legitimate. All the rest flows pretty obviously from there.


Thank you. I have more questions.
What other options are there besides positive infinity?


HUGE_VAL is required to expand into a positive double constant
expression (7.12p3). However, the C standard places no other
requirements on it, unless __STDC_IEC_559_ _ is #defined by the
implementation, in which case it's required to expand into an
expression with a value of positive infinity (F.9p2). As far as I can
see, if __STDC_IEC_559_ _ is not #defined, then it would be perfectly
legal to #define HUGE_VAL as DBL_MIN.
Could sqrt(HUGE_VAL) be undefined?
HUGE_VAL must be positive, and there's no positive number for which
sqrt() is undefined.
Is it allowed for sqrt(HUGE_VAL) to set errno to errange
if it returns HUGE_VAL?
According to 7.5p3, "The value of errno may be set to nonzero by a
library function call whether or not there is an error, provided the
use of errno is not documented in the description of the function in
this International Standard."

I could find no mention of errno in 7.12.7.5, which describes sqrt(),
so errno can be set to anything it wants. I'm not sure if that's the
intent; other parts of 7.12 describe the setting of errno in contexts
that apply to sqrt() - but that's not "in the description of the
function".

In particular, errno must be set to ERANGE if sqrt(HUGE_VAL) overflows
and math_errhandlin g & MATH_ERRNO is true. sqrt(HUGE_VAL) will
overflow, if and only HUGE_VAL is positive infinity.
Are any of the above 5 expressions
guaranteed to be true by the standard,
or is it merely the case that they are allowed to be true?
None of them are required by the standard unless __STDC_IEC_559_ _ is
#defined, in which case they are required in order to conform with IEC
559.
Suppose I want to write a 100% portable function for C89 and C99
that does exactly what log2 does. Could it be written this way?:

double l_og2(double x)
{
return log(x) / log(2);
}

The point being, Can I really on any funky values like HUGE_VAL,
which might be returned by log(x),
to not be altered by being divided by log(2)?
No.
Or would this be more portable?:

double l_og2(double x)
{
return x > 0 && DBL_MAX >= x ? log(x) / log(2) : log(x);
}
That looks good to me; but I might have missed some tricky detail.
It seems that most settings of errno to ERANGE are optional,
As, for instance, whenever 7.12p3 applies.
but for exp, it says that you do get them when the magnitude of x
is too large, not x, but the magnitude of x.
Is setting errno to ERANGE optional or mandatory for exp(-DBL_MAX)?
I think it would be a case of underflow.
On a system like mine, where HUGE_VAL is an infinity,
Is setting errno to ERANGE optional or mandatory for exp(-HUGE_VAL)?


If math_errhandlin g & MATH_ERRNO is true, then setting ERANGE is
mandatory for overflows and for cases where the exact value of a
function is infinite (7.12.1p4).

Nov 15 '05 #7
pete wrote:

On my system, the following five expressions are true:

(HUGE_VAL == HUGE_VAL / 2)
(1 / HUGE_VAL == 0)
(sqrt(HUGE_VAL) == HUGE_VAL)
Those are true if HUGE_VAL is +infinity (which it will be if IEEE-754
floating-point is being used).
(sqrt(-1) == HUGE_VAL)
(sqrt(-HUGE_VAL) == HUGE_VAL)
Those should be false. sqrt(negative) should be a NaN (Not-a-Number)
if IEEE-754 style floating-point is supported. And a NaN does not
compare equal to anything. In the general case, a domain error can
return any value, so +infinity is allowed.
and

sqrt(HUGE_VAL) doesn't set errno, but sqrt(-1) and sqrt(-HUGE_VAL) do.


Those are correct.
---
Fred J. Tydeman Tydeman Consulting
ty*****@tybor.c om Testing, numerics, programming
+1 (775) 358-9748 Vice-chair of J11 (ANSI "C")
Sample C99+FPCE tests: http://www.tybor.com
Savers sleep well, investors eat well, spenders work forever.
Nov 15 '05 #8
ro***********@y ahoo.com wrote:

pete wrote:
On my system, the following five expressions are true:

(HUGE_VAL == HUGE_VAL / 2)
(1 / HUGE_VAL == 0)
(sqrt(HUGE_VAL) == HUGE_VAL)
(sqrt(-1) == HUGE_VAL)
(sqrt(-HUGE_VAL) == HUGE_VAL)

and

sqrt(HUGE_VAL) doesn't set errno,
but sqrt(-1) and sqrt(-HUGE_VAL) do.

Does that all seem right?


Your implementation apparently
defines HUGE_VAL as a positive infinity,
which is legitimate. All the rest flows pretty obviously from there.


Thank you.

--
pete
Nov 15 '05 #9
Fred J. Tydeman wrote:

pete wrote:

On my system, the following five expressions are true:

(HUGE_VAL == HUGE_VAL / 2)
(1 / HUGE_VAL == 0)
(sqrt(HUGE_VAL) == HUGE_VAL)


Those are true if HUGE_VAL is +infinity (which it will be if IEEE-754
floating-point is being used).
(sqrt(-1) == HUGE_VAL)
(sqrt(-HUGE_VAL) == HUGE_VAL)


Those should be false. sqrt(negative) should be a NaN (Not-a-Number)
if IEEE-754 style floating-point is supported. And a NaN does not
compare equal to anything. In the general case, a domain error can
return any value, so +infinity is allowed.
and

sqrt(HUGE_VAL) doesn't set errno,
but sqrt(-1) and sqrt(-HUGE_VAL) do.


Those are correct.


Thank you.

For academic purposes,
I was hoping to be able to write a source code for a function sq_rt,
that would do exactly sqrt was supposed to do,
and have it be portable for C89 and C99.
Some standard functions like are easy to write like that,
like isdigit:

int is_digit(int c)
{
return c >= '0' && '9' >= c;
}

But, the math functions seem more complicated to write
portably because of the various macros in C99 which may
or may not be defined.

This is what I have so far:

double sq_rt(double x)
{
if (DBL_MAX >= x) {
if (x > 0) {
x = sqrt(x);
/*
** I've replaced my sqrt algortihm with the call to sqrt
** to simplify the appearence of the code
*/
} else {
if (0 > x) {
errno = EDOM;

#ifdef __STDC_IEC_559_ _
x = NAN;
#else
x = HUGE_VAL;
#endif

}
}
} else {
errno = ERANGE;
}
return x;
}

Does that seem about right?
Is this complicated enough enough
that I should try to acquire the standard for IEEE-754
if I want to do it right?
Followup set to c.l.c

--
pete
Nov 15 '05 #10

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

Similar topics

4
1555
by: Richard Tobin | last post by:
In a library I am writing, I want to use an errno-like mechanism for error returns. The error would probably be represented as a struct rather than just an integer. I don't have any multi-threaded programs, but others may who want to use my library. Most likely they'll be using a pthreads-compatible threads system. How can I best accommodate them? Presumably they will want the error object to be in per-thread storage, so to set and...
3
2435
by: Mac | last post by:
Is it legal to declare errno after you've included errno.h? For example: #include<errno.h> .... int main (void) {
4
9015
by: Paul Emmons | last post by:
If I am writing a function that sets errno according to a possible error condition, should it also set errno to 0 if it executes normally, or should it leave errno alone on success?
11
2528
by: Vijay Kumar R Zanvar | last post by:
> In <pan.2004.04.22.04.06.05.969827@bar.net> "Mac" <foo@bar.net> writes: > > >Is it legal to declare errno after you've included errno.h? > > > >For example: > > > >#include<errno.h> > > > >... > >
5
2364
by: Urs Beeli | last post by:
I have a question regarding errno. If I understand it correctly, including <errno.h> allows me to check "errno" for error values that some standard library functions may set. This brings up some questions: - As I understand it, most functions only set errno in an error case. I take this to mean, that on success, errno is not necessarily set to EOK and I would either need to set errno to EOK before calling the function in question to...
3
3390
by: eyalc1978 | last post by:
Hi Does someone knows what does errno 72 means I got this error when fwriting a structure and still the file is being created. I saw something on the net saying that this is caused when trying to access different file system
6
3477
by: Satish | last post by:
Hi , What is the behaviour as per ANSI standard for HUGE_VAL * 0.0? Also is there anywhere I can download ANSI standards for C/C++? Thanks, Satish
13
1937
by: Spiros Bousbouras | last post by:
Assume I'm writing a function which is going to set the value of errno if something went wrong but I also want to guarantee that errno will remain unchanged if the function completed its task succesfully. So at the beginning of my code I have something like int errsto = errno ; and just before every successful return I have errno = errsto ; Is this ok ?
22
2499
by: viza | last post by:
Hi all, A quick one - since errno is a lvalue, can I do: fread( & errno, sizeof errno, 1, fp ) ? TIA viza
0
8946
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8776
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
9449
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
8186
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
6735
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
6031
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
4550
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
4809
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3261
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system

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.