Questions tagged [float]

The tag has no usage guidance.

23 questions
Filter by
Sorted by
Tagged with
387 views

Pset2 - Readability: How to round a .5 number depending on the 2nd digit after the decimal point

I have written a solution for readability from pset2 but I'm having problems with one of the points: :( handles single sentence with multiple words expected "Grade 7\n", not "Grade 8\n". So I ...
786 views

pset 1 greedy: how to properly convert float to int

My problem is specifically how to convert the float that the user gives (dollars), into an accurate integer (cents). My code: printf("O hai! How much change is owed?\n"); float change_owed = ...
96 views

Why the printf function is able to print 28 decimal digits, if the float has only 24-bits mantissa?

What is exactly printing in the example in the week 2 lecture: float f = 1.0/10.0; printf("%.28f\n", f); // prints the following: 0.1000000014901161193847656250 I mean, if float is represented on ...
221 views

I can´t get (pset1) greedy/cash.c pennies to work properly!

i´ve been trying to figure out what is wrong with my code for the pset1 cash or greedy program, i know about float imprecision but when i print the last value for "ch" it prints .01000, thus meaning ...
102 views

Why floating point comparisons should be avoided?

Why floating point comparisons are inaccurate and should be avoided? For eg. this returns false 2.0 * 5.0 == 10.0 PS : By comparisons I mean equality and inequality, not < and >.
191 views

pset 1 cash get_positive_float not functioning as intended

I wanted to get the positive float using the get_positive_float function, which I declared later. However, it either accepts -0.99999 or rejects 0.999999. The code looks as follows: #include <...
844 views

Floating Point Imprecision

In Week 2 Lecture (the first one), Mr. Malan says a reason for floating - point imprecision (40:00). I don't get his reason. Sorry for the bad question, but please explain... Thanks in advance.
535 views

invalid operands to binary expression

error: invalid operands to binary expression ('char *' and 'double') printf ("Give me a postive number you want change for.\n"); scanf ("%f", &total_float); roundf ("%f" * 100.0 = "%d", &...
255 views

pset1 - “greedy” failing at one value

So, I just finished typing my "greedy" code for the fifth time and thought that this time it actually works, but when I run the check it fails at only one value 4.2. I'm going to post my code and hope ...
144 views

Pset1 Cash - Trying to round float value DURING computation and not after

My original code for this problem is as follows: int main(void) { float c = 0; //total coins float $= get_float("Change: "); while($ <= 0.0099999 && $>= 0.00000001) {$ = ...
110 views

PSET 4 - Filter Grayscale

I've managed to make Filter Grayscale work. It wasn't too complicated, I was just missing to cast each color to float as such; void grayscale(int height, int width, RGBTRIPLE image[height][width]) { ...
10 views

float rounds with %2.f

I'm working on pset2 with cash. But something interesting is happening if input is 6.876 and i printf that value of that variable I get 6.88 instead of 6.87. How can I get 6.87 instead of round 6.88? ...
107 views

22 views

Adding a code block to perform operations on float

I am currently on PSET 1 Cash and have my program asking for the amount of change required, not accepting negative values and now I want to add a code block to start performing the operations on my ...
139 views

Could someone explain the significance of double round in pset1 “greedy” (less comfortable)?

I've wrote the following which passes check50 and operates as intended: #include <stdio.h> #include <cs50.h> #include <math.h> ... int cents = round(change * 100); ... I've ...
49 views

Initialize the variable

I'm wondering why example one of the code doesn't work and example two does. The only change is the repetition of 'float'. The error I get are: unused variable and uninitialized variable. Would just ...
511 views

cs50 Problem Set 1: Greedy. float 0.01 problem

sorry for the long title I'm currently doing cs50's problem set 1's Greedy and I've run into an issue. It seems that every cent works except for one's that need to use .01 cent. I've changed my code ...
84 views

Int/Float Conversion

I fail to understand why C outputs a value of 0.000 instead of 0.625. Shouldn't it convert the result of a/b automatically to a float? If I instead write: float a,b; the output is 0.625. Can you tell ...
219 views

Pset1 greedy.c program error

I faced a problem when executing greedy.c. I think every thing is correct (or probably not), but every time I put \$4.2 as the input the program outputs 22 coins, which should be 18 ( 16 x 25c + 2 x ...
295 views

Readability in C passes tests on instructions page but when submitting, only 2/11 pass

I've written all of my code and tested that the letter, sentence and word output is counting correctly, which it seems to be. I even tested with all of the sentences on the instructions page and that ...
10k views

Problem with pset 1, cash

I get the correct answer for some values, i get the wrong answer for others. I didn't convert my change to cents because I figured I could just keep all my cents, dimes, quarters in decimal values too....