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 5 months ago.
Improve this question
In C, I am trying to figure out why my expression isn't always outputting what I am expecting it to. The expression is as follows:
z = (528*x*8)/(y*1.0e6)
In a calculator, this expression always gives me the expected result, but when ran in the program it doesn't always do as I expect it to. I don't remember the exact types of all of the variables, but I'm pretty sure that they are all unsigned ints. X and Y can both be pretty large numbers on their own. So, I'm wondering if it is possible that an integer overflow is happening before the expression is fully evaluated?
I'm wondering if it is possible that an integer overflow is happening before the expression is fully evaluated?
Yes, that is very possible.
For example, if x is 2000000, the subexpression 528*x*8 will overflow. Theoretically it would evaluate to 8448000000, but that's a 33-bit number. On most machines, type int is either 16 or 32 bits. (Probably 32 on yours, but still not big enough to hold a 33-bit number like 8448000000.)
Related
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
This question does not appear to be about programming within the scope defined in the help center.
Closed 2 years ago.
Improve this question
I have tried so many ways to calculate using this binomial theorem but I still couldn't find one:
The value of x and n is given for example b=0.5 and n=8
I know for the factorial we have to use loop but the numerator part is a little bit tricky.
Obviously I know how to code for (1+b)^n, but the question is still asking for the coding for binom theorem.
For example if the value of x is 0<x<1 and n is any positive integer, what will the value of (1+x)^n will be using the binomial theorem?
I understand that you know how to calculate the left side of the equation in programming.
I understand that you also know how to program the right side, apart from the problem that it is an infinite loop; but you want it to end at some point and have a result.
By the math theory ending early means a wrong result.
But in programming you will have problems with restricted precision of floating point math anyway. So you can take shortcuts to solve your problem.
In the comments you find recommendations how to do the calculation of each step efficiently. I will only focus on the end condition.
Write a loop calculating more and more precise steps.
End the loop when a freshly calculated (intermediate) result is the same as the previous one. With floating point representation having restricted precision that will sooner or later happen and the result will be within only one "minimal rounding" of the correct result.
Note:
In order to avoid the restricted precision getting in the way at the wrong place, I recommend to calculate the parts (as described in the recommendation in comments) in double and the intermediate results (those you compare for the loop condition) into a float variable.
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 programming in c and I'm using Netbeans IDE.
I'm trying to calculate the length of the sides of two triangles from their vertexes. However, when I try to use pow to calculate a square root, I get an error. I´ve included and when I use for example pow(2,2), it works. It's just this specific example that doesn't work. I've checked the parentheses like five times and they seem correct to me.
my code looks like this
(Don't mind the other variables, so far I'm just working with a1, a2, b1, b2 and c.)
You are using (1/2) as exponent in your call to the function pow. This is integer division and the result of 1/2 is 0. So this will not give you the square root.
Use 0.5 instead as exponent in the call to pow.
(1/2) will give you an integer value. and you need double.
you can use one of the following ways to avoid this situation.
(1.0/2) // (double/int) = double
// or (1/2.0)
((double)1/2) // 0.50000
// or ((double)1/(double)2)
Useful links :
C - Type Casting
Type Casting - C Programming
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 7 years ago.
Improve this question
Hello everyone I was going through a few programming questions and encounter one strange thing, The problem asked me to do some logic which is not relevant or nor I am asking the logic here, But the question involved the integers and said that I should keep in consideration that integer value be less than 10000000000
My doubt what data type to be used to store such ranges or, Lets just assume that some C program is used in some banking application which involves huge no of these magnitude, How do we store such huge no, Note: Even type 'long long' wont be able to store such huge no , Then how do we store such no ?
if possible use int64_t instead of long long that is defined in standard library header stdint.h. That holds 64 bit integers for sure. Larges number that can be represented is 2**63-1 and that is 9223372036854775807 (9e18). So it can hold 10000000000.
You could imagine the value stored in a database where numeric precision can be specified for data columns. But it's more likely that the particular value is specified to force you to think about how the algorithm or code would handle numeric overflow.
BTW, many systems use a 64 bit long long, which could hold the value you mention. Here's a great site to experiment with numbers and gain an intuitive feel for this:
http://www.wolframalpha.com/input/?i=2%5E64
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
Right now I'm trying to write a program, and one part of the it involves expanding an existing array by copying values from a previous array into an array with a larger size.
The way I'm doing this by using an int variable which is defined in a previous point int the program by user input.
int[x] array;
int[x + 1] array2;
Will this work, or do I have to initialize a separate int variable with value of x + 1?
The correct syntax is:
int array[x];
int array2[x+1];
C 1999 and later supports this (with the value of x determined at run time), although it is optional in C 2011. Some compilers (of questionable quality) do not support it.
The space available for objects of this sort is typically limited to one to eight mebibytes or so, and that space must also serve for other program needs, so it should be used only for small arrays.
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
I'm just beginning and I don't know how to change floating point form to exponential form.
A float is a float and has no other representation other than it's binary representation in memory. But you can change the way you print it to the console.
This can be done by specifying it in the printf function.
see printf
What you need is printf("%.2e",myfloat)
Those two "numbers" are simply the result of formatting the same floating point number in two different ways. No number conversion or casting is involved.
If you are concerned about internal representation - don't worry, it's all the same under the hood.
If you are going to print x = 1692.75 in desired form, use printf("%2.2e\n", x);
They are one of the same thing.
If you wish to print it see the manual page for printf
You don't have to change the internal representation of the float, which is binary and has nothing to do with what you see when you print the value.
If you just want to print your float you can you the printffamily of functions:
printf("%.2e", 1692.75);
should do the trick.