Increase precision on a double variable for small numbers [closed] - c

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 4 years ago.
Improve this question
I'm trying to perform the following operation:
double exponent = pow(2.0, -254)
The result I get is 'inf', the actual result is: 3.4544e-77, which is a very small number, I would guess that I could get '0' instead but I get 'inf'.
I need the actual result, is there a way to improve precision on a double? I have tried also long double without success.
I'm programming on C with Visual Studio.

In C, you can multiply by powers of two with scalb, so scalb(1, -254) is 2−254. You can also use powers of two in hexadecimal floating-point constants; 2−254 is 0x1p-254.
However, pow(2.0, -254) does not return infinity. If you attempted to print the return value, and “inf” was printed, there is an error in your source code.

Yes, the problem was in my source code, my original code is:
#include <math.h>
void main()
{
unsigned int scale = 1;
unsigned int bias = 255;
double temp = pow(2.0, scale - bias);
printf("Number is: %e\n", temp);
return;
}
which Im guessing the operation 'scale-bias' is overflowing since they are UINTS, thus getting me a 'inf', changing them to INT solves the issue:
#include <math.h>
void main()
{
int scale = 1;
int bias = 255;
double temp = pow(2.0, scale - bias);
printf("Number is: %e\n", temp);
return;
}

Related

The modf function in C [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 5 years ago.
Improve this question
I have been doing some research on the modf() function and I am having some trouble understanding on how to use it.
Code Example
#include<stdio.h>
#include<math.h>
int main ()
{
double x, fractpart, intpart;
x = 8.123456;
fractpart = modf(x, &intpart);
printf("Integral part = %lf\n", intpart);
printf("Fraction Part = %lf \n", fractpart);
return(0);
}
Confusion: The part I am getting confused on is the fractpart. I am having trouble understanding how it actually works because I only want to store the whole number part of a decimal number. Also, what is the point of the double.
First of, double is something really easy to understand. Float only stores seven digits, while a double stores between 15-16 digits. More detail here
Now for the modf. The modf in the fractpart =, it first checks x. Pretty much scanning it. After it does that, it stores everything before in the decimal point in inpart. After, what is remained is stored in fractpart which the decimal. To sum everything modf is like scanf. That is the reason why we put & before the intpart.
I only want to store the whole number part of a decimal number.
double modf(double value, double *iptr);
The modf functions break the argument value into integral and fractional parts, each of which has the same type and sign as the argument. They store the integral part in the object pointed to by iptr. C11dr §7.12.6.12 1
Simplified code follows. No need for fractpart or even intpart. Simply store the whole number portion of x back into x
#include<stdio.h>
#include<math.h>
int main () {
double x = 8.123456;
modf(x, &x);
printf("Integral part = %f\n", x);
}

Converting a float with many decimal places to a float with one decimal place only [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 7 years ago.
Improve this question
i want to know if there is a code that can convert float number like : 4.91820238 to a float with one decimal place only : 4.9 in C without using any libraries
P.S: I don't use print() function , I want to convert it and sotre its value ?
You can use the fmodf function in the standard math.h header.
Then subtract the result from the original.
#include <stdio.h>
#include <math.h>
int main(void) {
float f = 4.9182;
printf("%f", f - fmodf(f, 0.1));
return 0;
}
Edit:
I see from your comment that you are on a freestanding environment. So no standard library. But you can probably whip up you own function by consulting the example implementation on the reference site, and doing some inline assembly.
Just multiply by 10 and divide by 10, do this
float a = 4.91820238;
int i;
i = a*10;
num = i/10.0;
// num has 4.9 now
Remember the 10.0 during the divison, if you just divide it by 10 you'll just get the integer quotient of it.
Convert it to an integer type and back again:
float myRound(float f, int num_places) {
float factor = 1f;
for (i = 0; i < num_places; i++) {
factor *= 10f;
return ((long long)(factor * (f + .5f)))/factor;
}
Note that the code above is subject to the usual precision/range problems regarding floating point numbers.

Integration of Bessel functions in C++/Fortran [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 7 years ago.
Improve this question
How can I integrate an equation including bessel functions numerically from "0" to "infinity" in Fortran or/and C?
I did in matlab, but it's not true for larger inputs and after a specific values , the bessel functions give completely wrong results(there is a restriction in Matlab)
There's a large number of analytic results for various integrals of the Bessel functions (see DLMF, Sect. 10.22), including definite integrals over precisely this range. You'd be much better off, and almost certainly faster and more accurate, trying hard to recast your expression into something that's integrable and using an exact result.
Last time I had to do with such things, it was state of the art to do simple integration of the intervals defined by the zero crossings. That is in most cases relatively stable and if the integrand is approaching zero reasonable fast easy to do.
As a starting point for playing around I´ve included a bit of code. Of course you need to work on the convergence detection and error checking. This is no production code but I thought maybe it provides a starting point for you. Its using gsl.
On my iMac this code takes about 2 µs per iteration. It will not become faster by including a hardcoded table for the intervals.
I hope this is of some use for you.
#include <iostream>
#include <vector>
#include <gsl/gsl_sf_bessel.h>
#include <gsl/gsl_integration.h>
#include <gsl/gsl_sf.h>
double f (double x, void * params) {
double y = 1.0 / (1.0 + x) * gsl_sf_bessel_J0 (x);
return y;
}
int main(int argc, const char * argv[]) {
double sum = 0;
double delta = 0.00001;
int max_steps = 1000;
gsl_integration_workspace * w = gsl_integration_workspace_alloc (max_steps);
gsl_function F;
F.function = &f;
F.params = 0;
double result, error;
double a,b;
for(int n=0; n < max_steps; n++)
{
if(n==0)
{
a = 0.0;
b = gsl_sf_bessel_zero_J0(1);
}
else
{
a = n;
b = gsl_sf_bessel_zero_J0(n+1);
}
gsl_integration_qag (&F, // function
besselj0_intervals[n], // from
besselj0_intervals[n+1], // to
0, // eps absolute
1e-4,// eps relative
max_steps,
GSL_INTEG_GAUSS15,
w,
&result,
&error);
sum += result;
std::cout << n << " " << result << " " << sum << "\n";
if(abs(result) < delta)
break;
}
return 0;
}
You can pretty much google and find lots of Bessel functions implemented in C already.
http://www.atnf.csiro.au/computing/software/gipsy/sub/bessel.c
http://jean-pierre.moreau.pagesperso-orange.fr/c_bessel.html
https://msdn.microsoft.com/en-us/library/h7zkk1bz.aspx
In the end, these use the built in types and will be limited to the ranges they can represent (just as MATLAB is). At best, expect 15 digits of precision using double precision floating point representation. So, for large numbers, they will appear to be rounded. eg. 1237846464123450000000000.00000
And, of course, others on Stack Overflow have looked into it.
C++ Bessel function for complex numbers

how to find length of float number in C WITHOUT using strings...? [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
for example length of 1.54 is 4 including the decimal point. i need a C code for finding the length in the same way but without strings and also snprintf
Calculating the length of a floating-point number decimal representation doesn't make much sense, as its underlying representation is binary. For example 0.1 in decimal form yields an infinite number of digits in binary.
C has no intrinsic decimal type, so you will have to represent your number with some ad-hoc type, and shave off insignificant digits on the right. C-strings are not the worse choice for that.
The length is set by you at the moment you print it.
#include <stdio.h>
int main ()
{
float x = 0.2;
double y = 0.2;
int size_x = sizeof(float);
int size_y = sizeof(double);
printf("Variable x has a size of %i. But i can print if as different lengths: %f, %3.2f, %g, %e\n", size_x, x, x, x, x);
printf("Variable y has a size of %i. But i can print if as different lengths: %lf, %3.2lf, %lg, %le\n", size_y, y, y, y, y);
return 0;
}

append numbers C Programming [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
This question appears to be off-topic because it lacks sufficient information to diagnose the problem. Describe your problem in more detail or include a minimal example in the question itself.
Closed 8 years ago.
Improve this question
If I have the number 88 how would I add 00 to the end of and turn it into 8800? Bitwise shifts is the only way I can think of to do this but it doesn't work. It completely changes the numbers.
Bitwise shifts can only be used to multiply by powers of two, you simply want multiplication. Just run:
printf("%d", 88 * 100);
to print 8800.
If all you want to do is literally add 00 to the end of numbers you can instead do:
printf("%d00", 88);
You cannot do everything with bitwise shift operators alone. Its mathematically impossible to say it straight. But if you still insist you can do something like (88 << 6) + (88 << 5) + (88 << 2)
As a comment points out your answer can be obtained simply by multiplying your number by hundred.
So, you read from a file, and want to add two zeroes to it. Two ways I can see to do this: String-wise and Numerically.
String-wise, you can use
strcat(inputedText, "00");
(or just printf, or possible other solutions)
numerically, you can convert the inputted data from the file to an int, and multiply it by 100. If you need to print it, Vality's answer shows how to do that.
Live run: http://ideone.com/wiW5Zb
#include <stdio.h>
#include <stdint.h>
int64_t calc(int64_t value)
{
int64_t t = 1;
while(t < value)
t *= 10;
return t;
}
int64_t concatnum(int64_t a, int64_t b)
{
return (a * calc(b)) + b;
}
int main()
{
int a = 1000;
int b = 11;
int c = concatnum(a, b); //b must be greater 10.
printf("%d", c);
return 0;
}

Resources