473,804 Members | 2,134 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

comparing mantissa

Hi!
is there a better/faster way to compare mantissas of to real number then
in following code?

#include <stdio.h>
#include <stdlib.h>

int main(void) {
float a,b;
int test;

a=1.4234;
b=3.34;
test=((a-(int)a)>(b-(int)b));
printf("a=%f b=%f\n mantisa is bigger in a %d", a,b,test);

return EXIT_SUCCESS;
}

Thank you in advance
Jul 12 '07 #1
18 4122
Carramba wrote:
Hi!
is there a better/faster way to compare mantissas of to real number then
in following code?

#include <stdio.h>
#include <stdlib.h>

int main(void) {
float a,b;
int test;

a=1.4234;
b=3.34;
test=((a-(int)a)>(b-(int)b));
printf("a=%f b=%f\n mantisa is bigger in a %d", a,b,test);

return EXIT_SUCCESS;
}
Do you mean <math.hmodf() ? It doesn't look like you mean mantissa in
the numerical analysis sense.
Jul 12 '07 #2
Well, if you don't care about the exponenet and only care about the size
of the fraction, in all but the special cases (NaN, +/- infinity) you can
get a pointer to int and point it to the memory location that sores the number.
Shift the data that the pointer to int (really a float) points to left by
nine bits. Do the same for the other number and then compare the results.

But your "solution: still maintains the sign portion of the number. So,
-.222 < .111, by your specification -.222 should be greater than .111.
---Matthew Hicks

Hi!
is there a better/faster way to compare mantissas of to real number
then
in following code?
#include <stdio.h>
#include <stdlib.h>
int main(void) {
float a,b;
int test;
a=1.4234;
b=3.34;
test=((a-(int)a)>(b-(int)b));
printf("a=%f b=%f\n mantisa is bigger in a %d", a,b,test);
return EXIT_SUCCESS;
}
Thank you in advance

Jul 12 '07 #3

"Carramba" <ca******@examp le.comwrote in message
news:f7******** **@aioe.org...
Hi!
is there a better/faster way to compare mantissas of to real number then
in following code?

#include <stdio.h>
#include <stdlib.h>

int main(void) {
float a,b;
int test;

a=1.4234;
b=3.34;
test=((a-(int)a)>(b-(int)b));
printf("a=%f b=%f\n mantisa is bigger in a %d", a,b,test);

return EXIT_SUCCESS;
}

Thank you in advance
Yes, there is a better way, since the above will fail to give the correct
answer for negative numbers.
--
Fred L. Kleinschmidt
Boeing Associate Technical Fellow
Aero Stability and Controls Computing
Jul 12 '07 #4
In article <b6************ *************@n ews.ks.uiuc.edu >,
Matthew Hicks <md******@uiuc. eduwrote:
>Well, if you don't care about the exponenet and only care about the size
of the fraction, in all but the special cases (NaN, +/- infinity) you can
get a pointer to int and point it to the memory location that sores the number.
Shift the data that the pointer to int (really a float) points to left by
nine bits. Do the same for the other number and then compare the results.
You are assuming a specific floating point representation that might
be wildly completely wrong. For example on some (very real) systems,
float and double have the same representation, and if that representation
is IEEE 754 64 bit binary radix, then 9 bits of shift would not be
enough.

Shifting bits would get an answer in line with the IEEE 754 definition
of "mantissa", but the answer would be very different than the OP's
b - (int)b answer. For example, 2 and 8192 (and a number of other
numbers) have the same binary mantissa [with different binary exponents],
but 2.1 and 8192.1 have quite different binary mantissas.
--
I was very young in those days, but I was also rather dim.
-- Christopher Priest
Jul 12 '07 #5
Fred Kleinschmidt skrev:
"Carramba" <ca******@examp le.comwrote in message
news:f7******** **@aioe.org...
>Hi!
is there a better/faster way to compare mantissas of to real number then
in following code?

#include <stdio.h>
#include <stdlib.h>

int main(void) {
float a,b;
int test;

a=1.4234;
b=3.34;
test=((a-(int)a)>(b-(int)b));
printf("a=%f b=%f\n mantisa is bigger in a %d", a,b,test);

return EXIT_SUCCESS;
}

Thank you in advance

Yes, there is a better way,
Thank you for pointing out me to the right direction...
>since the above will fail to give the correct
answer for negative numbers.
Jul 12 '07 #6
In article <b6************ *************@n ews.ks.uiuc.edu >, Matthew
Hicks <md******@uiuc. eduwrote:
>Well, if you don't care about the exponenet and only care about the
size of the fraction, in all but the special cases (NaN, +/-
infinity) you can get a pointer to int and point it to the memory
location that sores the number. Shift the data that the pointer to
int (really a float) points to left by nine bits. Do the same for
the other number and then compare the results.
You are assuming a specific floating point representation that might
be wildly completely wrong. For example on some (very real) systems,
float and double have the same representation, and if that
representation
is IEEE 754 64 bit binary radix, then 9 bits of shift would not be
enough.
Yes, sorry I was in Java land where float means IEEE 754 single precision.
I was going to point out my assumptions but I thought that would have been
redundant. But, not in C.
Shifting bits would get an answer in line with the IEEE 754 definition
of "mantissa", but the answer would be very different than the OP's
b - (int)b answer. For example, 2 and 8192 (and a number of other
numbers) have the same binary mantissa [with different binary
exponents],
but 2.1 and 8192.1 have quite different binary mantissas.
Apparently you didn't bother reading the second part of my post, or the first
part even. I mentioned that my technique was only valid if he cared about
the mantissa. I also pointed out as others have, that his code is wrong
when dealing with negative numbers if he goes by his definition of what he
was trying to do. I think you are trying to ASSUME that he wanted something,
but that isn't what the OP asked for.
---Matthew Hicks
Jul 12 '07 #7
In article <b6************ *************@n ews.ks.uiuc.edu >,
Matthew Hicks <md******@uiuc. eduwrote:
>In article <b6************ *************@n ews.ks.uiuc.edu >, Matthew
Hicks <md******@uiuc. eduwrote:
>>Well, if you don't care about the exponenet and only care about the
size of the fraction,
>Shifting bits would get an answer in line with the IEEE 754 definition
of "mantissa", but the answer would be very different than the OP's
b - (int)b answer.
>Apparently you didn't bother reading the second part of my post, or the first
part even. I mentioned that my technique was only valid if he cared about
the mantissa.
What you actually -wrote- is quoted above. "[...] and only care about
the size of the fraction". "size of the fraction" is NOT the same
as the IEEE 754 definition of "mantissa". The OP's example
dealt with the decimal fractions, and you used the word "fraction"
yourself rather than clarifying that your shift proposal would deal
with the mantissa rather than the fraction. When you are dealing,
for example, with numbers in the 8192 range, the left-most bit of
the mantissa is nominating values in the 4096 range rather than
nominating a value in the range [0,1) as would be the case when
you use the word "fraction".

For very specific implementations , your algorithm could work for
comparing mantissas. But unless the OP has very specific precision
requirements, it would likely make more sense to use the standard C
function frexp() to extract the "normalized fraction and an integral
power of 2"; the drawback of that is that it works with double
rather than float and the mantissa of a double is -potentially-
completely different than the mantissa of a float representing
the same number. Historically, this did happen with more than
one major vendor, in the days before IEEE single precision
numbers became standardized (if I recall correctly, double
precision was standardized first and there was a noticable
lag before single precision was standardized.)
--
If you lie to the compiler, it will get its revenge. -- Henry Spencer
Jul 12 '07 #8
Matthew Hicks skrev:
Well, if you don't care about the exponenet and only care about the size
of the fraction, in all but the special cases (NaN, +/- infinity) you
can get a pointer to int and point it to the memory location that sores
the number. Shift the data that the pointer to int (really a float)
points to left by nine bits. Do the same for the other number and then
compare the results.

But your "solution: still maintains the sign portion of the number. So,
-.222 < .111, by your specification -.222 should be greater than .111.
I should have mentions that all number are >0; in any case this is a good
point to make shore it's covered for the future use.
>
---Matthew Hicks

>Hi!
is there a better/faster way to compare mantissas of to real number
then
in following code?
#include <stdio.h>
#include <stdlib.h>
int main(void) {
float a,b;
int test;
a=1.4234;
b=3.34;
test=((a-(int)a)>(b-(int)b));
printf("a=%f b=%f\n mantisa is bigger in a %d", a,b,test);
return EXIT_SUCCESS;
}
Thank you in advance

Jul 12 '07 #9
In article <f7**********@a ioe.org>, Carramba <ca******@examp le.comwrote:
>I should have mentions that all number are >0; in any case this is a good
point to make shore it's covered for the future use.
Okay, so now how about clarifying whether you mean "mantissa" in the
IEEE 754 binary floating point representation sense (if so,
use frexp()), or whether you mean "decimal fraction" as suggested
by your code (in which case, use modf()).
--
"No one has the right to destroy another person's belief by
demanding empirical evidence." -- Ann Landers
Jul 12 '07 #10

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

Similar topics

0
2572
by: JackRazz | last post by:
Is there a function to extract the mantissa and exponent from a double like the frexp function does in other languages (c++ & Python)? Thanks - JackRazz
5
5354
by: sankar | last post by:
Hi, I am using a Q14.18 value. There are tables used in my program which are being indexed by the exponent and mantissa parts of the corresponding floating point value. So how can I get the exponent and mantissa parts of a floating point number from its Q format representation.
2
3007
by: fzmaster | last post by:
Actually i need to know how to detect if a number is a perfect square. But i'v been thinking.. if i sqrt(x) and check if this result has mantissa... so it is not a interger...and surely not a perfect square. Well my question is... how can i verify if a number has mantissa??? i'm thx full for R help. bye. fzmaster
27
3872
by: Thomas Kowalski | last post by:
Hi everyone, To determine equality of two doubles a and b the following is often done: bool isEqual ( double a, double b ) { return ( fabs (a-b) < THRESHOLD ); } But this a approach usually fails if comparing doubles of different magnitude since it's hard or not possible to find a suitable threshold
18
2896
by: eman.abu.samra | last post by:
Hi all, i have encountered the strangest behavior. Check out this simple program: #include <stdio.h> int main() { double time = 1;
2
10013
by: Peng Yu | last post by:
Hi, I'm wondering if there is any standard function that computes mantissa a floating point number. I could use x - floor(x), suppose x is a floating number. But this involves an additional floating point operation (minus). Thanks, Peng
17
6816
by: D'Arcy J.M. Cain | last post by:
I'm not sure I follow this logic. Can someone explain why float and integer can be compared with each other and decimal can be compared to integer but decimal can't be compared to float? True True False This seems to break the rule that if A is equal to B and B is equal to C then A is equal to C.
6
3717
by: Terry Reedy | last post by:
Gerhard Häring wrote: The new fractions module acts differently, which is to say, as most would want. True Traceback (most recent call last): File "<pyshell#20>", line 1, in <module> F(1.0) File "C:\Program Files\Python30\lib\fractions.py", line 97, in __new__
0
9595
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
10603
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...
1
10356
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 most users, this new feature is actually very convenient. If you want to control the update process,...
0
10099
tracyyun
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, Z-Wave, Wi-Fi, 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 choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
9176
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
7643
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...
1
4314
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
2
3836
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
3003
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.