6
votes
Accepted
pset1 - "greedy" failing at one value
This is a very common problem and the whole point of greedy.c, so let me try and give the definitive answer.
Float values with a decimal part (to the right of the decimal, in case you weren't certain)...
4
votes
Accepted
Floating Point Imprecision
While it is easy to represent integers in a computer, it is different for floating point numbers. Most FP numbers cannot be precisely represented inside a computer. The reason is because numbers are ...
3
votes
Accepted
Pset2 - Readability: How to round a .5 number depending on the 2nd digit after the decimal point
Your problem isn't that round(7.535600) rounds to 8 (It should, and does), but that 7.535600 isn't the correct index value.
For that text, I'd expect these values:
96 letter(s)
23 word(s)
1 sentence(...
3
votes
I can´t get (pset1) greedy/cash.c pennies to work properly!
Perhaps you don't understand float imprecision fully enough. If you were to print the value of ch with more digits of precision, you would see what's happening. Try using the following:
printf("%0....
2
votes
Accepted
Why the printf function is able to print 28 decimal digits, if the float has only 24-bits mantissa?
[I posted this on reddit last year but rather than just linking it, I thought I'd repost. In that case, they had printed a few more decimals, but the principle is the same.]
A float value has 32 ...
2
votes
Accepted
Pset1 Cash - Trying to round float value DURING computation and not after
The code seems far more complicated than necessary, but this seems to be because you're trying to handle the imprecise float storage problem unsuccessfully.
This can be resolved by simply converting ...
2
votes
Accepted
cs50 Problem Set 1: Greedy. float 0.01 problem
Most of the problem is that no matter what you do, you keep putting the result back into change, a float. Floats have this nasty little problem that they don't store fractional parts of a number, the ...
1
vote
Accepted
PSET 4 - Filter Grayscale
The reason is that if the code is doing division with all integers, it will do integer division, not normal division.
Integer division will truncate the fractional part of the division. So, if the ...
1
vote
Accepted
float rounds with %2.f
I know of two ways, but neither can be done through how printf works.
The way I would do it would be to multiply the value by 100 and cast it to an int, which would cut off any remaining decimal ...
1
vote
Accepted
Int/Float Conversion
This is because when a/b is performed the result is o form int type which is 0 since the .625 part of the result is gone due to the expression a/b being an int. When the value a/b (i.e 0) is assigned ...
1
vote
In the buy function the variable float(stock_total) will not convert to float. Can someone explain how to fix it? Also what is causing the problem?
The value being coerced has mixed character data (the $ in the case of $208.43). Consider either sanitising the return value or revising the finance.db data type.
1
vote
Accepted
Adding a code block to perform operations on float
So you decided to make some functions, why not. The function definition would be like
return_type function_name(param1_type param1_name, param2_type param2_name, ...)
{
// body of the function
}
...
1
vote
Accepted
pset 1 cash get_positive_float not functioning as intended
I declared n as an int, not a float. That caused the problem.
1
vote
Problem with pset 1, cash
In addition to the float imprecision, every "while" loop in your code evaluates to true as the owing amount will always be greater than 1 cent, 5 cents, 10 cents, and 25 cents if it is greater than 25 ...
1
vote
Accepted
Problem with pset 1, cash
You’ve run into float imprecision, which is the main lesson of this problem. The specialization and walkthrough recommended that you multiply the user’s input by 100 and then round it, (there is a ...
1
vote
Accepted
Could someone explain the significance of double round in pset1 "greedy" (less comfortable)?
double is one of the types in C, like int and char. It's a double-precision floating point number. So
#include <math.h>
double round(double x);
means that the function takes a double and ...
1
vote
Accepted
Initialize the variable
It's pretty straightforward. In the second one, the code uses a single variable called c. No problems.
In the first one, by including float in the while statement, the code is redeclaring the ...
1
vote
Accepted
1
vote
invalid operands to binary expression
Line 1: OK
Line 2: If you have declared total_float as a float before this code snippet it's OK.
Line 3: That is not the correct use of roundf(). I would suggest you use man roundf on a terminal to ...
1
vote
Pset1 greedy.c program error
Did you try using round command after multiplying 4.2 by 100?
uma1966
Only top scored, non community-wiki answers of a minimum length are eligible