Hi there.
I am cross posting this on comp.lang.c as well: sorry for same.
The problem I am facing is as follows:
For example:
double a= 0.15;
double b=2.4;
const double VERYTINY =1.e10;
I know b/a = 16 and hence the remainder is zero; but I am not
able to find any suitable thing to encode it into in c.
for example (fmod(b,a)>VERY TINY) returns true!
Now for this particular instance, (fmodf(b,a)>VER YTINY)
does return false.
But now if
a=0.15;
b=4.5;
then fmodf and fmod both don't help...
any suggestions on this?
I was pointed to a reference on floating point arithmetic, where they talk
of ulps etc, but is there a small function or fix to deal with this problem
available somewhere?
thanks,
amit. 14 2607
"Amit Bhatia" <bh*****@nospam .com> wrote: The problem I am facing is as follows: For example:
Example of what? It makes more sense to state the
thing you want to give an example of, before giving
examples of it.
double a= 0.15; double b=2.4; const double VERYTINY =1.e10; I know b/a = 16 and hence the remainder is zero; but I am not able to find any suitable thing to encode it into in c.
Define "encode".
Define "it".
If you want to code b/a in C, the code is:
b/a
for example
Example of what? Please give some clue as to the thing
you're trying to give an example OF, before giving
examples!
(fmod(b,a)>VERY TINY) returns true!
It returns false on my system. However, it seems to me
you're pushing the granularity of type double to the max.
Even if you WERE off by one part per 1e10, so what?
That's one part per ten billion. Who cares?
But if you really DO care, try acquiring a compiler that
supports type "long double". Or, perhaps, use a computer
with a 64bit processor and compiler that defines "double"
to be 64 bits. Even if 4 or so of those bits were used
for sign and exponent, you'd still have a granularity of
about 1 part per quintillion (1e18). More precision than
you'll EVER need.
Now for this particular instance, (fmodf(b,a)>VER YTINY) does return false.
What is fmodf? That's not a part of the C or C++ std.
libraries. I don't have it on my compiler (DJGPP) either.
But now if a=0.15; b=4.5; then fmodf and fmod both don't help...
I get b/a = 30 and (fmod(b,a)>VERY TINY) = false.
What do YOU get?
any suggestions on this?
Suggestions on what, precisely?
I was pointed to a reference on floating point arithmetic, where they talk of ulps etc,
What's "ulps"?
is there a small function or fix to deal with this problem available somewhere?
What problem are you trying to fix?

Cheers,
Robbie Hatley
Tustin, CA, USA lo**********@pa cbell.net http://home.pacbell.net/earnur/
On Sun, 11 Jul 2004 01:17:09 0700, Robbie Hatley <lonewolfintj at pacbell
dot net> wrote: I was pointed to a reference on floating point arithmetic, where they talk of ulps etc,
What's "ulps"?
ULP stands for unit in the last place. Its a measure of rounding error and
a common enough term in floating point arithmetic.
john
On Sat, 10 Jul 2004 23:08:01 0500, Amit Bhatia <bh*****@nospam .com> wrote: Hi there. I am cross posting this on comp.lang.c as well: sorry for same. The problem I am facing is as follows: For example: double a= 0.15; double b=2.4; const double VERYTINY =1.e10; I know b/a = 16 and hence the remainder is zero; but I am not able to find any suitable thing to encode it into in c. for example (fmod(b,a)>VERY TINY) returns true! Now for this particular instance, (fmodf(b,a)>VER YTINY) does return false. But now if a=0.15; b=4.5; then fmodf and fmod both don't help...
any suggestions on this?
Maybe use a bigger value for VERYTINY.
Maybe use integral or rational aritmetic.
Maybe use fixed point arithmetic.
I was pointed to a reference on floating point arithmetic, where they talk of ulps etc, but is there a small function or fix to deal with this problem available somewhere?
There is not easy answer to the problem of floating point rounding errors.
The simplest thing is not to write code that depends on any particular
exactness of floating point arithmetic. Obviously this is not always
possible but it is possible more often than people think.
So the asnwer to your question really depends on what kind of problem you
are actually trying to solve.
john
On Sat, 10 Jul 2004 23:08:01 0500 in comp.lang.c++, Amit Bhatia
<bh*****@nospam .com> wrote, Hi there. I am cross posting this on comp.lang.c as well: sorry for same.
In fact you did so a few days ago and got some good tips.
The problem I am facing is as follows: For example: double a= 0.15; double b=2.4; const double VERYTINY =1.e10;
b = two and two fifths. Fifths can not be represented exactly in base
two floating point format, so this number cannot be stored exactly.
a is 16 times that, so of course has a similar approximation.
And so on. Never expect any form of exactitude from any floating point
operation, and your life will be less frustrating.
I know b/a = 16 and hence the remainder is zero; but I am not able to find any suitable thing to encode it into in c.
It may be close to zero, or it may be just less than 0.15.
If you persist in this folly then I guess you need to test for both
possibilities.
Amit Bhatia <bh*****@nospam .com> wrote in message news:<cc******* ***@news.ks.uiu c.edu>... Hi there. I am cross posting this on comp.lang.c as well: sorry for same. The problem I am facing is as follows: For example: double a= 0.15; double b=2.4; const double VERYTINY =1.e10; I know b/a = 16 and hence the remainder is zero; but I am not able to find any suitable thing to encode it into in c. for example (fmod(b,a)>VERY TINY) returns true! Now for this particular instance, (fmodf(b,a)>VER YTINY) does return false. But now if a=0.15; b=4.5; then fmodf and fmod both don't help...
any suggestions on this? I was pointed to a reference on floating point arithmetic, where they talk of ulps etc, but is there a small function or fix to deal with this problem available somewhere?
thanks, amit.
int main() {
double a = 0.15;
double b = 2.4;
const double VERYTINY =1.e10;
printf("%d", (fmod(b,a) > VERYTINY));
return 0;
}
This gives me false!
Amit Bhatia <bh*****@nospam .com> wrote in message news:<cc******* ***@news.ks.uiu c.edu>... Hi there. I am cross posting this on comp.lang.c as well: sorry for same. The problem I am facing is as follows: For example: double a= 0.15; double b=2.4; const double VERYTINY =1.e10; I know b/a = 16 and hence the remainder is zero; but I am not able to find any suitable thing to encode it into in c. for example (fmod(b,a)>VERY TINY) returns true! Now for this particular instance, (fmodf(b,a)>VER YTINY) does return false. But now if a=0.15; b=4.5; then fmodf and fmod both don't help...
any suggestions on this? I was pointed to a reference on floating point arithmetic, where they talk of ulps etc, but is there a small function or fix to deal with this problem available somewhere?
thanks, amit.
Try the following loop
cout.precision( 20);
cout << b << endl << a << endl;
while(b>a)
b = a;
cout << b << endl << a << endl;
and see the value of a and b
Your problem is that the method of not comparing floating
point values but comparing their difference to a certain limit
does not work for modulo arithmetic. The reason is simple.
Assume x(mod y) = 0
That is, x = yp
In your case, x and y are floating point values,
while p is an integer. Since p is an integer, you
can say x is divisible by y.
But a computer cannot store floating point numbers exactly.
so a floating point number x is actually stored as (x+dx)
or as (xdx). In your case, 4.5 can be exactly stored, but
neither can 2.4 or 0.15 . Now consider what happens when you
divide (x+dx) or (xdx) by y. (x,y,p > 0)
(x+dx)(mod y) = dx
This is ok, your comparison will work in this case, as your
VERYTINY is > dx
However, (xdx)(mod y) = (ypdx)(mod y)
= (y(p1) + y  dx)(mod y) = ydx
This ydx is definitely mmuch greater than your VERYTINY
and therefore you do not get the desired result.
I'll give you an example with integers.
Consider dividing 57 by 19. 57(mod 19) = 0
58(mod 19) = 1 ( x+dx case)
56(mod 19) = 18 (xdx case)
So you should write
( fmod(b,a)<VERYT INY  (afmod(b,a))<VERY TINY )
Arijit
When using floatingpoint, usually we don't test for equality, instead do
the kind of test you do (fmod(b, a) > VERYTINY)...
But if we take a look at the case in particular, the test doesn't fail on my
machine, and I suspect the > VERYTINY doesn't fail in yours, except for
negative numbers. That is,
2.4 can only be finitely represented in binary as 2.3999... that is
10.0110011(0011 )
0.15 as 0.14(9) (or 0.00100110011(0 011))
in my machine, perhaps yours too, fmod(2.4, 0.15) is exactly 0. This is not
surprising if we examine the mantissa bits of both numbers, they are equal.
but 4.5 does have a finite binary representation as 100.1
but fmod(4.5, 0.15) is only 1.66533e16, and so is smaller than 1e10... and
I don't know why the test should fail....
Since the case of the denominator being represented exactly implies that a
multiple is represented exactly too
So, given best possible representations of the input values the test
(abs(fmod(b, a)) < VERYTINY)) should work.
One exception arises when input values are themselves approximations, and,
in particular, the dividend is slightly less than it would take to be an
exact multiple. In that case, the remainder will be less than, but very
close to the denominator. So, we must test the result also against it being
very close to the denominator.
So, the full test, that should work for all cases would be (assuming we want
to know about approximate multiples):
(abs(fmod(b,a)) < VERYTINY)  (abs(fmod(b,a)  a) < VERYTINY)
Now, the value you choose for VERYTINY, depends on the application and how
many operations you have done on the input values. Of course, because the
result of fmod is in absolute value always less than a, if you know your
inputs are positive numbers, you can drop both abs function calls.
Miguel Ramos What is fmodf? That's not a part of the C or C++ std. libraries. I don't have it on my compiler (DJGPP) either.
actually fmodf does exist, it is part of the C99 standard, and most modern
compilers have it.
but I think we shouldn't be very harsh, it's obvious he meant modf.
"Miguel" <mi****@exstare .com> wrote in message news:40******** *************** @news.telepac.p t... What is fmodf? That's not a part of the C or C++ std. libraries. I don't have it on my compiler (DJGPP) either. actually fmodf does exist, it is part of the C99 standard,
"fmodf" is a not a keyword in C or C++, nor is it
a part of their standard libraries, according to my
reading. I found reference to "fmodf" in the
documentation for my compiler (DJGPP), where it is
listed as a "nonANSI extention to fmod". So unless
fmodf has been added to the standard libraries very
recently, it's not standard.
and most modern compilers have it.
Most compilers have lots of added nonANSI functions.
Useful, but not standard.
it's obvious he meant modf.
Probably.

Cheers,
Robbie Hatley
Tustin, CA, USA lo**********@pa cbell.net http://home.pacbell.net/earnur/ This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics 
by: Roger Leigh 
last post by:
Hello,
I'm writing a fixedprecision floating point class, based on the ideas
in the example fixed_pt class in the "Practical C++ Programming" book
by Steve Oualline (O' Reilly). This uses a long int to store the
value, and the precision (number of decimal points) is variable (it's
a templated class):
template <size_t _decimal_places =...

by: Mantorok Redgormor 
last post by:
What have some of you guys read to have a solid understanding of how
floatingpoint numbers are represented or handled by the processor and
what the difference between single and double precision is?
I found this: http://docs.sun.com/source/8063568/ncg_goldberg.html
Not sure if this is what I should be reading? Maybe a more authoritive...

by: cody 
last post by:
no this is no trollposting and please don't get it wrong but iam very
curious why people still use C instead of other languages especially C++.
i heard people say C++ is slower than C but i can't believe that. in pieces
of the application where speed really matters you can still use "normal"
functions or even static methods which is...

by: j0mbolar 
last post by:
C supports single precision floating point and double precision
floating point but does it support fixed floating point? i've read
that fixed floating point is more accurate than single precision
floating point when dealing with dollars and cents.

by: Shawn 
last post by:
Hello all,
I apologize as I am sure this has probably been dealth with before...
but I am doing an exercise from "Practical C Programming" and I have
been unable to get it to work perfectly due to problems with floating
point arithmetic and I am looking for a way to solve it. See the code
below...
Given a certain amount of change (below...
 
by: Vinoth 
last post by:
I'm working in an ARM (ARM9) system which does not have Floating point
coprocessor or Floating point libraries. But it does support long long int
(64 bits).
Can you provide some link that would discuss about ways to emulate floating
point calculations with just long int or long long int. For eg., if i've a
formula X=(1b)*Y + b*Z in floating...

by: Satpreet 
last post by:
I'm looking to simulate the behavior of a digital hardware arithmetic
block in a C/C++ program.
I was just wondering if there are any libraries (with datatypes and
overloaded operators etc.) available for arithmetic operations on
Fixed/BlockFloating Point variables ??
Alternatively could anyone throw me a pointer to sample code if...

by: ma740988 
last post by:
template <class T> inline bool isEqual( const T& a, const T& b,
const T epsilon = std::numeric_limits<T>::epsilon() )
{
const T diff = a  b;
return ( diff <= epsilon ) && ( diff >= epsilon );
}
int main()
{
std::deque<double> pt ;

by: Robert Gamble 
last post by:
9899:1999 5.1.2.3 Example 4 reads:
"EXAMPLE 4 Implementations employing wide registers have to take care
to honor appropriate semantics. Values are independent of whether they
are represented in a register or in memory. For example, an implicit
spilling of a register is not permitted to alter the value. Also, an
explicit store and load is...

by: marktang 
last post by:
ONU (Optical Network Unit) is one of the key components for providing highspeed 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...

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...
 
by: Oralloy 
last post by:
Hello folks,
I am unable to find appropriate documentation on the type promotion of bitfields 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. ...

by: Hystou 
last post by:
Overview:
Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...

by: tracyyun 
last post by:
Dear forum friends,
With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, ZWave, WiFi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the...

by: agi2029 
last post by:
Let's talk about the concept of autonomous AI software engineers and nocode 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...

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...

by: adsilva 
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
 
by: muto222 
last post by:
How can i add a mobile payment intergratation into php mysql website.
 