Hot answers tagged

6 votes
Accepted

Difficulty rounding on Greedy in pset1

You'll want to use the round function on that amount*100. Given that it's simply a matter of syntax, I don't mind just telling you. It's as simple as int rounded_number = round(other_number*100); ...
user avatar
  • 18.4k
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(...
user avatar
  • 18.4k
3 votes
Accepted

Pset1 Greedy Help Please

It's exactly the issue of lack of rounding. If x is 4.2, for example, x*100 is 419 because 4.2 can't be expressed exactly in binary. It's actually 4.1999999nnnn. When you then multiply by 100 and ...
user avatar
  • 18.4k
3 votes
Accepted

Modulo not working in Greedy

If you tried typecasting to int, still the error would have existed because you are still storing the value in a float. So its basically like, original value = 12.27 round()'s output = 1227.00 ...
user avatar
  • 3,326
3 votes

Modulo not working in Greedy

Modulo Operator ( % ) requires both the operands to be of type int . In your code change is a floating point variable. Use an integer variable to store the rounded value.
user avatar
2 votes
Accepted

pset1 greedy rounding

float roundf(float n) is declaration of round function, and here I want to just use it. So I should type float rounded = roundf(n);
user avatar
2 votes
Accepted

Still Need Help. Pset1 Geedy with round()

It looks like you have a couple of misconceptions in your code. Mostly, you need to understand how integer math works compared to normal math. For instance, when the content of a float is stored in an ...
user avatar
  • 64.2k
2 votes
Accepted

PSet1 Cash returning implicitly declaring library function 'roundf' with type 'float (float)'

Have you read the man page for the function? Maybe there's something your should include in your code that isn't there? ;-) If this answers your question, please click on the check mark to accept. ...
user avatar
  • 64.2k
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 ...
user avatar
  • 64.2k
1 vote
Accepted

How should rounding be handled in pset6 readability?

If you simply use round it will round the expected way (up if it's >= 0.5, down otherwise. Python 3.9.1 (default, Jun 15 2021, 23:45:14) [GCC 9.3.0] on linux Type "help", "copyright&...
user avatar
  • 18.4k
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 ...
user avatar
  • 18.4k
1 vote
Accepted

Number imprecision & Rounding - C$50 Finance

This answer may help: pset7, cash on index.html sometimes giving many decimals (eg.4882.139999999999) And also this answer on how to implement: https://stackoverflow.com/questions/6036082/call-a-...
user avatar
1 vote

Pset1 Greedy Help Please

The point of the exercise has been missed - floats are frequently not precisely stored. If you want to see the problem, try printing cents to a precision of 20 digits after the decimal. Also, for ...
user avatar
  • 64.2k
1 vote

Compilation error on function round in greedy.c, pset1

Did you use make? Or did you manually type clang [flags]? If it is the latter you must link the math.h library with the -lm flag.
user avatar
  • 1,223
1 vote
Accepted

Why should I use round in greedy?

You understand the round function correctly, it does round a float to the nearest whole number. The point of the exercise is to understand how floats are stored in the computer and how to adapt to ...
user avatar
  • 64.2k
1 vote

Pset1 greedy.c program error

Did you try using round command after multiplying 4.2 by 100? uma1966
user avatar
  • 99
1 vote

pset1 greedy rounding

You need to round it after multiplying it by 100. And I'd suggest using round() rather than roundf(). You don't want a float for your number of cents. You want to be dealing in integers.`
user avatar
  • 18.4k
1 vote
Accepted

Getting a inifinite do while loop while using printf() and round()

The problem is that variables are in different scope. Scope refers to the rules in C which determine how variables are access. Variables can only be accessed in the scope in which they are defined. In ...
user avatar
  • 2,625
1 vote
Accepted

roundf function gives error in clang but runs smoothly in g++

Regarding the first error b=(int)roundf(float(a*100)); should be following, b=(int)roundf((float)(a*100)); However, typecasting to float is not needed as 'a' is already in float. Roundf is from ...
user avatar

Only top scored, non community-wiki answers of a minimum length are eligible