Hot answers tagged

6

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); (using your own variables, of course) Be sure to #include the library where the round() function is defined. I'll leave that to you to find. Hint: reference....


3

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(s) L: 417.391296 S: 4.347826 Score: 7.455652 Grade 7 Perhaps you are running into problems in your intermediate calculations. Have you made sure that you ...


3

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 store in an integer, it will be truncated to 419. That's why the round function exists. round(x*100) will round the value and give you 420 in that example.


3

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 Typecasted result = 1227 Storing the typecasted value back in a float variable = 1227.00 And modulo operations don't work with floating point numbers. So, its ...


3

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.


2

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);


2

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 int, the fractional part of the number is truncated, not rounded. Further, consider what happens when you subtract fractional numbers from an int. Try running ...


2

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. Let's keep up on forum maintenance. ;-)


2

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 dollars and cents in a float to cents only and storing it in an int. It's not that hard. First, remember that round functions round the input to the nearest ...


1

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 returns a double. You can pass it a float and it will handle that. So in your usage: int cents = round(change * 100); You are rounding the value of change * ...


1

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-python-function-from-jinja2 For me using the app.jinja statement worked when I put it at the bottom of application.py, and thus the function worked fine in index....


1

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 each of your calculations, try first storing the result as a float and print them out to 20 decimals. It should quickly become apparent where the problem lies. ...


1

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.


1

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 the situation. In this case, you're starting with a two digit decimal float that represents dollars and fractions of dollars and need to ultimately work with ...


1

Did you try using round command after multiplying 4.2 by 100? uma1966


1

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.`


1

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 this case there are two different scopes, the main function, and the calc function. It is clear from the code in the calc function, that the intention is to ...


1

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 math.h library, you need to link that library while compiling. Try following: clang -o greedy greedy.c -lm '-lm' tells the linker to use math library. Try this ...


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