comparing mantissa

C

Carramba

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
 
T

Tim Prince

Carramba said:
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.h> modf()? It doesn't look like you mean mantissa in
the numerical analysis sense.
 
M

Matthew Hicks

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
 
F

Fred Kleinschmidt

Carramba said:
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.
 
W

Walter Roberson

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

Matthew Hicks

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
 
W

Walter Roberson

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

Carramba

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

Walter Roberson

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()).
 
C

Carramba

Walter Roberson skrev:
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.

why is it different comparing two mantissas or two fraction?
if fraction of a > b then mantissa for a should be bigger then b's ?
since mantissa (significand) is considered to by integer or a fractions,
witch in comparison are the same?

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";

wouldn't the call to the function frexp() be more resource costly for my
purpose? since (for know) comparison is only for real number > 0? and
even if it would some lines would easy fix cases there number is < 0.

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.

do you mean that the length of the mantissa would differ? and the last
significant number may by different?

Historically, this did happen with more than
 
U

user923005

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;

}

Your are not comparing mantissas, but I guess that is not what you
want to do anyway.
If you really did want to compare mantissas, the right way to separate
mantissa and exponent is frexp():
http://www.opengroup.org/onlinepubs/007908799/xsh/frexp.html

What are you really trying to accomplish?
 
A

Army1987

Your are not comparing mantissas, but I guess that is not what you
want to do anyway.
Maybe "mantissa" has more meanings than you believe.
If you really did want to compare mantissas, the right way to separate
mantissa and exponent is frexp():
http://www.opengroup.org/onlinepubs/007908799/xsh/frexp.html

What are you really trying to accomplish?
He's trying to compare fractional parts, which I would do with
a - floor(a). Yes, "mantissa" is antiquate with this meaning, but
so it is with the meaning you're thinking about (for which
nowadays "significand" is used).
 
U

user923005

Maybe "mantissa" has more meanings than you believe.



He's trying to compare fractional parts, which I would do with
a - floor(a). Yes, "mantissa" is antiquate with this meaning, but
so it is with the meaning you're thinking about (for which
nowadays "significand" is used).

In the C language, there is exactly one meaning for mantissa, the one
that I used.
 
K

Keith Thompson

user923005 said:
In the C language, there is exactly one meaning for mantissa, the one
that I used.

Citation, please? I find no occurrence of the word "mantissa" in the
C99 standard.
 
U

user923005

[...]
In the C language, there is exactly one meaning for mantissa, the one
that I used.

Citation, please? I find no occurrence of the word "mantissa" in the
C99 standard.

I stand before thee ... busted.
I simply assumed that the standard used the same verbiage as my posix
document.
 
C

Carramba

Keith Thompson skrev:
Citation, please? I find no occurrence of the word "mantissa" in the
C99 standard.
isn't mantissa just another (old) word for significand? anyway it's
stated so in wikipedia.
but off course one should use correct terms witch are relevant now.
 
A

Army1987

Keith Thompson skrev:
isn't mantissa just another (old) word for significand? anyway it's
stated so in wikipedia.
but off course one should use correct terms witch are relevant now.
If with your original post you were trying to compare
significands, you were almost as misguided as you could possibly
have ever been.
 
K

Keith Thompson

Carramba said:
Keith Thompson skrev:
isn't mantissa just another (old) word for significand? anyway it's
stated so in wikipedia.
but off course one should use correct terms witch are relevant now.

There are several meanings of the word "mantissa", and the OP didn't
clearly state which one he was using.
 
A

Army1987

There are several meanings of the word "mantissa", and the OP didn't
clearly state which one he was using.
But the expressions he wrote (a - (int)a) make somewhat clear
which meaning he intended.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,431
Messages
2,571,679
Members
48,796
Latest member
Greg L.

Latest Threads

Top