Click here to Skip to main content
15,890,947 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi!
At first: its my second week in programming, Im an idiot, but its necessary on my studies.
I have to make a program, which will show the smallest floating point number with 32 bits. And it mus be like that: I start with one, while loop and divide by two to the end (a little more than zero).

What I have tried:

#include <stdio.h>

int main(void) {
float last = 1.0;
while(last > 0.0) {
last = (last / 2.0);
}
while (last = 0) {
printf("%f", last*2);
}

return 0;
}
Posted
Updated 29-Nov-19 2:50am
Comments
Patrice T 28-Nov-19 15:22pm    
And you have a question or a problem ?
Lilla_My___ 28-Nov-19 15:29pm    
i don't know, why its like that, why it doesn't work...

Look at your second loop:
C++
while (last = 0) {
   printf("%f", last*2);
   }
Since you don't change the value of last inside the loop, if it ever got in - which it won't - it could never exit.

But it will never enter the second loop because '=' is an assignment operator, not a comparison operator. And in C, zero values are treated as false.

I'd strongly suggest that you spend a little time getting used to the debugger - it would have shown you this very, very quickly...
 
Share this answer
 
In addition to the problems that Griff mentioned, the final output wouldn't work anyway. If last is 0, multiplying it by 2 will still give you 0.

You'll need to use two variables so that you can keep a copy of the previous step.
float last = 1.0;
float temp = last / 2.0;
while (temp > 0.0)
{
    last = temp;
    temp = last / 2.0;
}

printf("%f", last);
 
Share this answer
 
Quote:
i don't know, why its like that, why it doesn't work...

Your code do not behave the way you expect, or you don't understand why !

There is an almost universal solution: Run your code on debugger step by step, inspect variables.
The debugger is here to show you what your code is doing and your task is to compare with what it should do.
There is no magic in the debugger, it don't know what your code is supposed to do, it don't find bugs, it just help you to by showing you what is going on. When the code don't do what is expected, you are close to a bug.
To see what your code is doing: Just set a breakpoint and see your code performing, the debugger allow you to execute lines 1 by 1 and to inspect variables as it execute.

Debugger - Wikipedia, the free encyclopedia[^]

Mastering Debugging in Visual Studio 2010 - A Beginner's Guide[^]
Basic Debugging with Visual Studio 2010 - YouTube[^]

1.11 — Debugging your program (stepping and breakpoints) | Learn C++[^]

The debugger is here to only show you what your code is doing and your task is to compare with what it should do.
 
Share this answer
 
C
float x = 1.f;

while (x / 2.f > 0.f)
  x /= 2.f;

printf("%g\n", x);
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900