what is the correct way to calculate the "double's range"? - double

In 64 bits platform, the double's range are following:
-2.22507e-308 ~ +2.22507e-308
-1.79769e+308 ~ +1.79769e+308
and I followed the IEEE754 standard and tried to calculate the double's range by following equation,
x = (-1)^s * (1+fraction) * 2^(exponent-bias)
So I tried to calculate in 64 bits platform, but I am not sure the following whether it is right?
Since the bias is 1024, the max value of double can be calculated by following:
(1-2^-52) * 2^(2^11 - 1024) = 2.22507e-308
It seems right...
but when I calculate the min value of double by the same way
(1-2^-52) * 2^-(1024-1)) = 1.1125369292536007e-308,
1024 -1 (minus 1 is for zero bit?))
the result is not correct... so I wonder what is correct way to calculate double's range by hand.
Thank in advance.

Max (substitute any sensible type):
std::numeric_limits<double>::max()
Min (called "lowest" in C++11 because min meant something else, see comments):
std::numeric_limits<double>::lowest()

Related

Specify maximum width of printed value in MATLAB

The field width parameter of sprintf and fprintf (e.g., "n" in "sprintf('%nf',1234)") specifies the minimum width of the field. This means that shorter numbers will be padded but longer numbers will be allowed to extend beyond the specified field width when this is necessary to maintain the specified precision. I am looking for a way to specify the maximum field width such that width supersedes precision.
For example, I would want 1234.56, 1234.56789, and 1.2345 to have the width of 8 characters (including the decimal) and therefore print as 1234.560, 1234.567, and 1.234500, respectively.
Here is a similar question, but without a MATLAB-specific solution
Specifying maximum printf field width for numbers (truncating if necessary)?
I feel like someone has to have encountered this problem before, but I was unable to locate anything relevant. Please provide a link if a similar question exists. Thanks
You can use floor(log10()) to determine how many non floating digits you have:
X = [1234.56, 1234.56789, 1.2345]
n = 8 % number of total digits.
% We create an array of string containing the format:
%[ '%.4f\n'
% '%.4f\n'
% '%.7f\n' ]
form = strcat('%.',num2str(n-1-floor(real(log10(X))).'),'f\n')
% We concatenate this array into one single string and we print the result:
fprintf(reshape(form.',1,[]),X)
Or you can use a for loop (using the same logic):
for x = X
form = strcat('%.',num2str(7-floor(real(log10(x)))),'f\n');
fprintf(form,x)
end
And we obtain:
1234.5600
1234.5679
1.2345000
Edit:
I use floor(log10()) instead of ceil(log10()) so the code won't fail if a number is part of the set 10^n (1,10,100...).
And now this snippet can deal with negative floating number since I only take the real part of the log10 output.
if you don't mind truncating instead of rounding the hidden digits, try
S = num2str( X(:), '%-10.9f'); % creates a left-justified character-array
fprintf( '%s\n', string( S(:,1:7) ) )
(X would need to be checked for values too big.)

Calculate the percentage in Matlab

I want to calculate the percentage of accuracy. I have the code below. But it give unexpected output like this "The accuracy is 2.843137e+01x37".
While expected result is "The accuracy is 28.43%"
y %Amount of correct data
j %Amount of all data
a = 'The accuracy is %dx%d.';
percent = '%.0f%%';
format short
acc = 100 * double(y/j);
sprintf (a,acc)
How to fix it?
Any help would be so much appreciated.
Thank you.
You almost have what you expected, just put it together the right way.
The correct format specifier for 28.43% is %.2f%%. This gives you two digits after the decimal point and adds the %-sign at the end. You have that defined in the variable percent, except that .0 should be .2 for two digits as you have written in the expected result. If you look closely, you'll realize that percent is never used.
Let's come to the conclusion. Change the format specifier to the following:
a = 'The accuracy is %.2f%%';
That's all you need to do. The line defining percent can be omitted as well as format short unless you need this for something later on.
Something important regarding the cast to double: What you currently have just casts the result. If necessary, do the cast individually to y and/or j before the division. Probably you don't need any casting in your case.
The whole code with an assumption for y and j is:
y = 28.43137; %// Amount of correct data
j = 100; %// Amount of all data
a = 'The accuracy is %.2f%%';
acc = 100 * (y/j); %// no cast
% acc = 100 * (double(y)/double(j)); %// with cast
sprintf(a,acc);
Output:
ans =
The accuracy is 28.43%
Try,
a = 'The accuracy is %f.';
acc = 100 * double(y/j);
sprintf (a,acc)

What is the range of values in a sample of type AudioUnitSampleType?

AudioUnitSampleType is a SInt32. When I log it to the console I get readings roughly between -22000 and 22000.
Can someone explain what those values represent? How do they describe the wave form? I suppose because they are positive and negative that the axis is in the center of the wave form.
How would I convert the values to decibel such that 0 dB is loudest?
Although the sample type is a 32-bit int, the values you received will generally fit in a 16-bit int (ie, 32767..-32767). 32767 (or floating point 1.0) corresponds to 0dB.
After converting the sample to floating point value, the power in decibels looks something like this:
double sampleValue = (double)intSampleValue / 32767.0;
double db = 20.0 * log10(value);
I've done the above calculations as double precision to avoid overflow.

Is there equivalent function to fminbnd for maximum value?

I am doing homework in Matlab, calculating numeric integration using different methods like simpson, etc. I need to find value n from formulas for error of method like this one for rectangle method
E = (b-a)/24 * max f''(x) * ((b-a)/n)^2
n = sqrt(((b-a)^3 * max f''(x)) / 24 * E)
problem is finding function for finding maximum value of f''(x) in range a to b.
I have only found fminbnd, which calculates minimum value of function in range. Is there function which would calculate maximum value?
Look for the minimum of -f''(x) (the negative of the function you want to maximize).

How can I cast variables in matlab with fixed floating point

Is it possible convert a double variable to a float (single in Matlab) with fixed floating point?
For example
x = 10.023213032130123021302130210331232132103312321
to
x = 10.0231
Thank you !
First convert to single:
X = single(Y)
And than apply round() to get fixed format:
X = round(10^N*X) / 10^N;
to get N digits behind the decimal point