473,466 Members | 3,167 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

Ceil double problem

Why do I get NO as result from this ??

double du = (double)3.1415;
du = ceil(du);
if(du == (double)4)
printf("YES\n");
else
printf("NO\n");

Mar 2 '06 #1
20 2282

mi**************@gmail.com wrote:
Why do I get NO as result from this ??

double du = (double)3.1415;
du = ceil(du);
if(du == (double)4)
printf("YES\n");
else
printf("NO\n");


I get YES (mingw/gcc). Your machine may not be able to represent 4.0
exactly. Your implementation of `ceil()` may be broken. Have you
included <math.h>? Did you try to print out `du` before and after
`ceil()`? I may have missed some other possibilities...

--
BR, Vladimir

Mar 2 '06 #2
mi**************@gmail.com writes:
Why do I get NO as result from this ??

double du = (double)3.1415;
du = ceil(du);
if(du == (double)4)
printf("YES\n");
else
printf("NO\n");


I don't know. I'd expect it to print YES, and it did so when I
compiled your code -- but only after I put it into an actual program.

You've posted a code fragment, not a program, so strictly speaking it
doesn't produce any output at all.

The only explanation I can think of is that your actual program
doesn't have the required "#include <math.h>". This could cause the
compiler to assume that ceil() returns an int rather than a double,
resulting in undefined behavior.

If you still haven't figured it out, post a complete compilable
program that reproduces the problem.

Incidentally, the casts are unnecessary. A floating-point literal
such as 3.1415 is already of type float; in the comparison, it's
simpler and cleaner to use "4.0" rather than "(double)4)".

--
Keith Thompson (The_Other_Keith) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.
Mar 2 '06 #3
"Vladimir S. Oka" <no****@btopenworld.com> wrote:
mi**************@gmail.com wrote:
Why do I get NO as result from this ??

double du = (double)3.1415;
du = ceil(du);
if(du == (double)4)
printf("YES\n");
else
printf("NO\n");


I get YES (mingw/gcc). Your machine may not be able to represent 4.0
exactly.


If so, it's broken.

Richard
Mar 2 '06 #4

Richard Bos wrote:
"Vladimir S. Oka" <no****@btopenworld.com> wrote:
mi**************@gmail.com wrote:
Why do I get NO as result from this ??

double du = (double)3.1415;
du = ceil(du);
if(du == (double)4)
printf("YES\n");
else
printf("NO\n");


I get YES (mingw/gcc). Your machine may not be able to represent 4.0
exactly.


If so, it's broken.


Not if it uses bas 3 arithmetic. ;-)
Do doubles have to be encoded in base 2?

--
BR, Vladimir

Mar 2 '06 #5
"Vladimir S. Oka" <no****@btopenworld.com> wrote:
Richard Bos wrote:
"Vladimir S. Oka" <no****@btopenworld.com> wrote:
mi**************@gmail.com wrote:
> Why do I get NO as result from this ??
>
> double du = (double)3.1415;
> du = ceil(du);
> if(du == (double)4)
> printf("YES\n");
> else
> printf("NO\n");

I get YES (mingw/gcc). Your machine may not be able to represent 4.0
exactly.


If so, it's broken.


Not if it uses bas 3 arithmetic. ;-)
Do doubles have to be encoded in base 2?


Oddly enough, no. Or rather, oddly enough, the base doesn't have to be a
power of 2; base 16 FP maths would be reasonable on some systems.
However, DBL_DIG must be greater than or equal to 10; 4.0 only has 1
decimal.

Richard
Mar 2 '06 #6
"Vladimir S. Oka" <no****@btopenworld.com> writes:
Richard Bos wrote:
"Vladimir S. Oka" <no****@btopenworld.com> wrote:
> mi**************@gmail.com wrote:
> > Why do I get NO as result from this ??
> >
> > double du = (double)3.1415;
> > du = ceil(du);
> > if(du == (double)4)
> > printf("YES\n");
> > else
> > printf("NO\n");
>
> I get YES (mingw/gcc). Your machine may not be able to represent 4.0
> exactly.


If so, it's broken.


Not if it uses bas 3 arithmetic. ;-)
Do doubles have to be encoded in base 2?


Even in base 3, 4.0 is represented as 11.0. All small integer values
should be exactly representable in any sane floating-point
representation. (This is something of a circular definition[*],
since I wouldn't consider any representation that doesn't do this to
be "sane".)

Floating-point equality comparison is fraught with peril, but I would
expect it to work properly for simple values like 4.0, as long as the
value 4.0 is derived (as it is here) without any inexact operations.
[*] See "definition, circular".

--
Keith Thompson (The_Other_Keith) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.
Mar 2 '06 #7
Keith Thompson wrote:
"Vladimir S. Oka" <no****@btopenworld.com> writes:
Richard Bos wrote:
"Vladimir S. Oka" <no****@btopenworld.com> wrote:
> mi**************@gmail.com wrote:
> > Why do I get NO as result from this ??
> >
> > double du = (double)3.1415;
> > du = ceil(du);
> > if(du == (double)4)
> > printf("YES\n");
> > else
> > printf("NO\n");
>
> I get YES (mingw/gcc). Your machine may not be able to represent
> 4.0 exactly.

If so, it's broken.


Not if it uses bas 3 arithmetic. ;-)
Do doubles have to be encoded in base 2?


Even in base 3, 4.0 is represented as 11.0. All small integer values
should be exactly representable in any sane floating-point
representation. (This is something of a circular definition[*],
since I wouldn't consider any representation that doesn't do this to
be "sane".)

Floating-point equality comparison is fraught with peril, but I would
expect it to work properly for simple values like 4.0, as long as the
value 4.0 is derived (as it is here) without any inexact operations.


You are, of course, right. I didn't really think this through.

It'd still be interesting to know why OP's implementation failed to do
this properly.

--
BR, Vladimir

Your depth of comprehension may tend to make you lax in worldly ways.

Mar 2 '06 #8
On 2006-03-02, Richard Bos <rl*@hoekstra-uitgeverij.nl> wrote:
"Vladimir S. Oka" <no****@btopenworld.com> wrote:
Richard Bos wrote:
> "Vladimir S. Oka" <no****@btopenworld.com> wrote:
>
> > mi**************@gmail.com wrote:
> > > Why do I get NO as result from this ??
> > >
> > > double du = (double)3.1415;
> > > du = ceil(du);
> > > if(du == (double)4)
> > > printf("YES\n");
> > > else
> > > printf("NO\n");
> >
> > I get YES (mingw/gcc). Your machine may not be able to represent 4.0
> > exactly.
>
> If so, it's broken.


Not if it uses bas 3 arithmetic. ;-)
Do doubles have to be encoded in base 2?


Oddly enough, no. Or rather, oddly enough, the base doesn't have to be a
power of 2; base 16 FP maths would be reasonable on some systems.
However, DBL_DIG must be greater than or equal to 10; 4.0 only has 1
decimal.


DBL_DIG doesn't mandate an exact representation, it only requires that
all numbers to that precision be uniquely representable.

My c89 draft says:

* number of decimal digits of precision, $left floor~(p~-~1)~times~{
log sub 10 } b~right floor ~+~ left { lpile { 1 above 0 } ~~ lpile {
roman "if " b roman " is a power of 10" above roman otherwise }$

According to my best interpretation of eqn syntax, that becomes:

* number of decimal digits of precision, floor((p-1)*log[10]b)+(1 if b
is a power of 10, 0 otherwise)

neqn+nroff mangles this into (blank lines removed)

* number of decimal digits of precision, |_(_\bp-1)xlog10_\bb_|+{
is a power of 10 0
\b\b\b\b\botherwise

Where 'p' is DBL_MANT_DIG and 'b' is FLT_RADIX.
Mar 2 '06 #9
On 2006-03-02, Keith Thompson <ks***@mib.org> wrote:
Incidentally, the casts are unnecessary. A floating-point literal
such as 3.1415 is already of type float; in the comparison, it's
simpler and cleaner to use "4.0" rather than "(double)4)".


Well, it's actually of type double. Of course, your point is still made.

A literal of type float ends in 'f'. say, 3.142f
Mar 2 '06 #10
mi**************@gmail.com wrote:
Why do I get NO as result from this ??

double du = (double)3.1415;
du = ceil(du);
if(du == (double)4)
printf("YES\n");
else
printf("NO\n");

Allow me..

#include <stdio.h>
#include <math.h>

int main(void)
/*
{
double du = (double) 3.1415;
du = ceil(du);
if (du == (double) 4)
printf("YES\n");
else
printf("NO\n");
return 0;
}
*/
{
double du = 3.1415;
du = ceil(du);
if (du == 4.0)
printf("YES\n");
else
printf("NO\n");
return 0;
}

The first instance (commented out) produces YES. The second instance
without annoying casts also produces YES. What are you doing or not
doing that produces NO?

--
Joe Wright
"Everything should be made as simple as possible, but not simpler."
--- Albert Einstein ---
Mar 3 '06 #11
Jordan Abel <ra*******@gmail.com> writes:
On 2006-03-02, Keith Thompson <ks***@mib.org> wrote:
Incidentally, the casts are unnecessary. A floating-point literal
such as 3.1415 is already of type float; in the comparison, it's
simpler and cleaner to use "4.0" rather than "(double)4)".


Well, it's actually of type double. Of course, your point is still made.

A literal of type float ends in 'f'. say, 3.142f


You're right of course. I *meant* double, and I was sure I had
written double, but it became "float" somewhere between my brain and
my keyboard. Thanks.

--
Keith Thompson (The_Other_Keith) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.
Mar 3 '06 #12
Joe Wright wrote:

mi**************@gmail.com wrote:
Why do I get NO as result from this ??

double du = (double)3.1415;
du = ceil(du);
if(du == (double)4)
printf("YES\n");
else
printf("NO\n");

Allow me..

#include <stdio.h>
#include <math.h>

int main(void)
/*
{
double du = (double) 3.1415;
du = ceil(du);
if (du == (double) 4)
printf("YES\n");
else
printf("NO\n");
return 0;
}
*/
{
double du = 3.1415;
du = ceil(du);
if (du == 4.0)
printf("YES\n");
else
printf("NO\n");
return 0;
}

The first instance (commented out) produces YES. The second instance
without annoying casts also produces YES. What are you doing or not
doing that produces NO?


I'm giving 2 : 1 odds he didn't bother to #include <math.h>. He
then cleverly concealed this by posting only a portion of his code,
thus avoiding immediate sensible answers.

Here's the key to my other car, which is at home. It won't start
when I turn the key. Why?

--
"If you want to post a followup via groups.google.com, don't use
the broken "Reply" link at the bottom of the article. Click on
"show options" at the top of the article, then click on the
"Reply" at the bottom of the article headers." - Keith Thompson
More details at: <http://cfaj.freeshell.org/google/>
Also see <http://www.safalra.com/special/googlegroupsreply/>
Mar 3 '06 #13
In article <44****************@news.xs4all.nl> rl*@hoekstra-uitgeverij.nl (Richard Bos) writes:
"Vladimir S. Oka" <no****@btopenworld.com> wrote:

....
Not if it uses bas 3 arithmetic. ;-)
Do doubles have to be encoded in base 2?


Oddly enough, no. Or rather, oddly enough, the base doesn't have to be a
power of 2; base 16 FP maths would be reasonable on some systems.
However, DBL_DIG must be greater than or equal to 10; 4.0 only has 1
decimal.


But this does *not* imply that 4.0 is exactly representable. It only
tells something about conversion and backconversion to and from a
decimal representation.
--
dik t. winter, cwi, kruislaan 413, 1098 sj amsterdam, nederland, +31205924131
home: bovenover 215, 1025 jn amsterdam, nederland; http://www.cwi.nl/~dik/
Mar 3 '06 #14
"Dik T. Winter" <Di********@cwi.nl> writes:
In article <44****************@news.xs4all.nl> rl*@hoekstra-uitgeverij.nl (Richard Bos) writes:
> "Vladimir S. Oka" <no****@btopenworld.com> wrote:

...
> > Not if it uses bas 3 arithmetic. ;-)
> > Do doubles have to be encoded in base 2?

>
> Oddly enough, no. Or rather, oddly enough, the base doesn't have to be a
> power of 2; base 16 FP maths would be reasonable on some systems.
> However, DBL_DIG must be greater than or equal to 10; 4.0 only has 1
> decimal.


But this does *not* imply that 4.0 is exactly representable. It only
tells something about conversion and backconversion to and from a
decimal representation.


I think it does, indirectly. In that, there is not a legal (integer>2)
radix in which 4.0 could not be exactly represented, if there are
enough digits in the significand to meet the DBL_DIG
requirement, and the range of possible exponents is as demanded
by the standard.

-Micah
Mar 3 '06 #15
On 2006-03-03, Micah Cowan <mi***@cowan.name> wrote:
legal (integer>2) radix


Chapter and verse.
Mar 3 '06 #16
Jordan Abel <ra*******@gmail.com> writes:
On 2006-03-03, Micah Cowan <mi***@cowan.name> wrote:
legal (integer>2) radix


Chapter and verse.


Oops. Sorry, of course I meant >= 2, or >1. Obviously, 2 is an obvious
choice on many platforms.

5.2.4.2.2#1.
Mar 3 '06 #17
On 2006-03-03, Micah Cowan <mi***@cowan.name> wrote:
Jordan Abel <ra*******@gmail.com> writes:
On 2006-03-03, Micah Cowan <mi***@cowan.name> wrote:
> legal (integer>2) radix
Chapter and verse.


Oops. Sorry, of course I meant >= 2, or >1. Obviously, 2 is an obvious
choice on many platforms.


I didn't see that, I was questioning that it had to be an integer. and,
not, say, e.
5.2.4.2.2#1.


Ah. I was looking at the definition of FLT_RADIX itself - i'd missed
that. It's not clear that c89 says this, though it was probably
intended.
Mar 3 '06 #18
In article <87************@mcowan.barracudanetworks.com> Micah Cowan <mi***@cowan.name> writes:
"Dik T. Winter" <Di********@cwi.nl> writes:

....
> Oddly enough, no. Or rather, oddly enough, the base doesn't have to be a
> power of 2; base 16 FP maths would be reasonable on some systems.
> However, DBL_DIG must be greater than or equal to 10; 4.0 only has 1
> decimal.


But this does *not* imply that 4.0 is exactly representable. It only
tells something about conversion and backconversion to and from a
decimal representation.


I think it does, indirectly. In that, there is not a legal (integer>2)
radix in which 4.0 could not be exactly represented, if there are
enough digits in the significand to meet the DBL_DIG
requirement, and the range of possible exponents is as demanded
by the standard.


I do not think so. The standard (at least C89) does not require the
actual implemented floating-point arithmetic to follow the model.
The model is only used to explain the constants, if the actual
implementation is different, appropriate constants must be given.
As the footnote tells us:
"The floating-point model is intended to clarify the description of
each floating-point characteristic and does not require the
floating-point arithmetic of the implementation to be identical."
--
dik t. winter, cwi, kruislaan 413, 1098 sj amsterdam, nederland, +31205924131
home: bovenover 215, 1025 jn amsterdam, nederland; http://www.cwi.nl/~dik/
Mar 7 '06 #19
"Dik T. Winter" <Di********@cwi.nl> writes:
In article <87************@mcowan.barracudanetworks.com> Micah Cowan <mi***@cowan.name> writes:
> "Dik T. Winter" <Di********@cwi.nl> writes:

...
> > > Oddly enough, no. Or rather, oddly enough, the base doesn't have to be a
> > > power of 2; base 16 FP maths would be reasonable on some systems.
> > > However, DBL_DIG must be greater than or equal to 10; 4.0 only has 1
> > > decimal.
> >
> > But this does *not* imply that 4.0 is exactly representable. It only
> > tells something about conversion and backconversion to and from a
> > decimal representation.

>
> I think it does, indirectly. In that, there is not a legal (integer>2)
> radix in which 4.0 could not be exactly represented, if there are
> enough digits in the significand to meet the DBL_DIG
> requirement, and the range of possible exponents is as demanded
> by the standard.


I do not think so. The standard (at least C89) does not require the
actual implemented floating-point arithmetic to follow the model.
The model is only used to explain the constants, if the actual
implementation is different, appropriate constants must be given.
As the footnote tells us:
"The floating-point model is intended to clarify the description of
each floating-point characteristic and does not require the
floating-point arithmetic of the implementation to be identical."


Recall that footnotes are not normative, however.

In any case, I think the intention of that foot note is to remind us
that floating point arithmetic is merely "as-if" it followed that
model.

In any case, I don't think that the standard meant to allow
non-integer bases, or it wouldn't have made the point in disallowing
them. Given that fact, and the fact that representing 4.0 only
requires at most 2 significand digits (only base 3 requires more than
1) and an exponent value of at most 2 (only for base 2: 3 and 4
require 1, all others can use 0), I don't see how you can make
even a theoretical case for a conforming floating-point implementation
that can fail to represent it.
Mar 7 '06 #20
In article <87************@mcowan.barracudanetworks.com> Micah Cowan <mi***@cowan.name> writes:
"Dik T. Winter" <Di********@cwi.nl> writes: ....
> > But this does *not* imply that 4.0 is exactly representable. It only
> > tells something about conversion and backconversion to and from a
> > decimal representation.
.... I do not think so. The standard (at least C89) does not require the
actual implemented floating-point arithmetic to follow the model.
The model is only used to explain the constants, if the actual
implementation is different, appropriate constants must be given.
As the footnote tells us:
"The floating-point model is intended to clarify the description of
each floating-point characteristic and does not require the
floating-point arithmetic of the implementation to be identical."


Recall that footnotes are not normative, however.


Yes, I know, it is just a clarification of what the standard itself
already does state.
In any case, I think the intention of that foot note is to remind us
that floating point arithmetic is merely "as-if" it followed that
model.

In any case, I don't think that the standard meant to allow
non-integer bases, or it wouldn't have made the point in disallowing
them.
The standard does not disallow them, as long as the arithmetic can be
modelled with an integer base. You may note that the radix given
is not necessarily the actual radix of the base arithmetic, nor is
it necessary that the base arithmetic is floating-point. The only
requirement is that it can be modelled as if it follows such a model,
and indeed, nearly every arithmetic can be modelled as such.
Given that fact, and the fact that representing 4.0 only
requires at most 2 significand digits (only base 3 requires more than
1) and an exponent value of at most 2 (only for base 2: 3 and 4
require 1, all others can use 0), I don't see how you can make
even a theoretical case for a conforming floating-point implementation
that can fail to represent it.


That is all irrelevant as the standard allows all operations to be
as inexact as you wish, the only requirement is about the round-trip
conversion.
--
dik t. winter, cwi, kruislaan 413, 1098 sj amsterdam, nederland, +31205924131
home: bovenover 215, 1025 jn amsterdam, nederland; http://www.cwi.nl/~dik/
Mar 7 '06 #21

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

Similar topics

4
by: Jack | last post by:
I have a problem that I can't seem to solve. I have checked the round, ceil and floor functions and they don't seem to do what I want. I have an entry like this <?php $tax = .065; $ad2day =...
5
by: SpaceCowboy | last post by:
I'm using Sun ONE Studio 4 update 1, Community Edition with Nokia Developer's Suite integration. I'm developing a J2ME project with MIDP. I wanted to use the java.math.floor() function, but...
6
by: RobG | last post by:
I am writing a script to move an absolutely positioned element on a page by a factor using style.top & style.left. The amount to move by is always some fraction, so I was tossing up between...
9
by: PengYu.UT | last post by:
Hi, The usually integer division will round the result to the biggest integet smaller than the float version division.For example, 10/3 = 3. I'm wondering if there is any easy way to round it...
8
momotaro
by: momotaro | last post by:
#include <stdio.h> int myCeil(double); main() { int ans; double x; printf("enter your num: "); scanf("%lf", &x);
14
by: yansong1990 | last post by:
Uhm..it's my 1st time using this fuction...could someone provide anexample for me please? thanks
13
by: ptn | last post by:
Hi everyone, I was messing around with math.h and I got this error: """ /tmp/ccefZYYN.o: In function `digcount': itos.c:(.text+0x103): undefined reference to `log10' itos.c:(.text+0x111):...
7
by: MrIncognito | last post by:
Am I using this correctly? This line of code is supposed to find the number of bytes in one line of a 16 color bitmap, but it doesn't seem to work most of the time. long linesize = ceil((double)...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
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,...
0
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...
0
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,...
0
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...
0
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...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
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 ...

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.