Re: Asymmetry in CoreAudio representation?
Re: Asymmetry in CoreAudio representation?
- Subject: Re: Asymmetry in CoreAudio representation?
- From: "AUDIOSYNTH.COM" <email@hidden>
- Date: Fri, 5 Apr 2002 00:15:05 -0600 (CST)
On Thu, 4 Apr 2002, Doug Wyatt wrote:
>
- multiplying and dividing by 32767 which would introduce rounding error
>
(IEEE floating point numbers are base-2 fractions and so integers of up
>
to 24 bits can be converted to and from 32-bit floats perfectly by
>
multiplying/dividing by the appropriate integral power of 2)
Actually you can convert 24 bit values to and from floats by 1/2^23 OR
1/2^23-1 OR ANY constant value (!) and _still_ retain all 24 bits
on conversion to and from. try it:
These functions exhaustively test all 24 bit values, converting them to
and from floats using normalization factors of 2^24 and 2^24-1 and an
arbitrarily chosen float value, and determines if the bits on input are
ever not equal to the bits on output.
All tests pass. (sorry about the word wrap).
void floatbittest1();
void floatbittest1()
{
printf("test 1 start\n");
int itwotothe23 = 1L << 23;
float twotothe23 = (float)itwotothe23;
float oneovertwotothe23 = 1./twotothe23;
for (int in = -itwotothe23; in < itwotothe23; ++in) {
float fin = (float)in;
float normal = fin * oneovertwotothe23; // convert to normal range
if (normal < -1. || normal > 1.) {
printf("range failed %d %.8g\n", in, normal); // check range
}
float fout = normal * twotothe23;
int out = (int)fout;
if (in != out) {
printf("bits not preserved %d %d\n", in, out);
}
}
// if we get to here without other printfs then bits were preserved.
printf("test 1 done\n");
}
void floatbittest2();
void floatbittest2()
{
printf("test 2 start\n");
int itwotothe23 = 1L << 23;
float twotothe23minus1 = (float)itwotothe23 - 1.;
float oneovertwotothe23minus1 = 1./twotothe23minus1;
for (int in = -itwotothe23; in < itwotothe23; ++in) {
float fin = (float)in;
float normal = fin * oneovertwotothe23minus1; // convert to normal
range
if (normal < -1. || normal > 1.) {
printf("range failed %d %.8g\n", in, normal); // check range
// this one we expect to be below -1 for the asymmetrical
negative value -2^23
}
float fout = normal * twotothe23minus1;
int out = (int)fout;
if (in != out) {
printf("bits not preserved %d %d\n", in, out);
}
}
// if we get to here without other printfs then bits were preserved.
printf("test 2 done\n");
}
void floatbittest3();
void floatbittest3()
{
printf("test 3 start\n");
int itwotothe23 = 1L << 23;
float weird = (float)9726982.9782;
float oneoverweird = 1./weird;
for (int in = -itwotothe23; in < itwotothe23; ++in) {
float fin = (float)in; // coerce to float
float normal = fin * oneoverweird; // convert to normal range
if (normal < -1. || normal > 1.) {
printf("range failed %d %.8g\n", in, normal); // check range
}
float fout = normal * weird; // convert back
int out = (int)fout; // coerce to int
if (in != out) {
printf("bits not preserved %d %d\n", in, out);
}
}
// if we get to here without other printfs then bits were preserved.
printf("test 3 done\n");
}
--
--- james mccartney email@hidden <
http://www.audiosynth.com>
SuperCollider - a real time synthesis programming language for the PowerMac.
<
ftp://www.audiosynth.com/pub/updates/SC2.2.14.sea.hqx>
_______________________________________________
coreaudio-api mailing list | email@hidden
Help/Unsubscribe/Archives:
http://www.lists.apple.com/mailman/listinfo/coreaudio-api
Do not post admin requests to the list. They will be ignored.