Dividing negative number in C [duplicate]
This question already has an answer here:
C program to convert Fahrenheit to Celsius always prints zero
8 answers
Hi, i am absolute newbie in programming. I am starting with learning C by book "C Programming Language (2nd Edition)" and stuck in very first example where we get exercise to write simple program that prints values of temperatures from lower to upper in 2 columns (tabs) that contains Celsius a Fahrenheit.
I'v get problem because trying to edit those code for:
- Celsius is main system.
- Steps measured dynamically by dividing lower on any given number.
And all work perfectly while i am using integers variables.
#include <stdio.h>
main()
int celcius, farenheit;
int lower, upper, step;
lower = -273.15;
upper = 0;
step = lower / -10; // Dividing lower temperature by given number
celcius = lower;
while (celcius <= upper)
farenheit = celcius * 9/5 + 32;
printf("%dt%dn", celcius, farenheit);
celcius = celcius + step;
But goes to absolutely random numbers when i try using float or double variables for more precise result: (There is code and output in terminal)
#include <stdio.h>
main()
float celcius, farenheit;
float lower, upper, step;
lower = -273.15;
upper = 0;
step = lower / -10; // Dividing lower temperature by given number
celcius = lower;
while (celcius <= upper)
farenheit = celcius * 9/5 + 32;
printf("%dt%dn", celcius, farenheit);
celcius = celcius + step;
Output:
1610612736 1073741824
1073741824 1073741824
-1073741824 1073741824
1073741824 536870912
-1073741824 536870912
1073741824 0
-2147483648 0
-2147483648 -2147483648
536870912 -1610612736
-2147483648 0
So what happened behind that number magic and how to get this to work?
c integer output floating negative-number
marked as duplicate by Lundin
StackExchange.ready(function()
if (StackExchange.options.isMobile) return;
$('.dupe-hammer-message-hover:not(.hover-bound)').each(function()
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');
$hover.hover(
function()
$hover.showInfoMessage('',
messageElement: $msg.clone().show(),
transient: false,
position: my: 'bottom left', at: 'top center', offsetTop: -7 ,
dismissable: false,
relativeToBody: true
);
,
function()
StackExchange.helpers.removeMessages();
);
);
);
Nov 12 '18 at 7:44
This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.
|
show 3 more comments
This question already has an answer here:
C program to convert Fahrenheit to Celsius always prints zero
8 answers
Hi, i am absolute newbie in programming. I am starting with learning C by book "C Programming Language (2nd Edition)" and stuck in very first example where we get exercise to write simple program that prints values of temperatures from lower to upper in 2 columns (tabs) that contains Celsius a Fahrenheit.
I'v get problem because trying to edit those code for:
- Celsius is main system.
- Steps measured dynamically by dividing lower on any given number.
And all work perfectly while i am using integers variables.
#include <stdio.h>
main()
int celcius, farenheit;
int lower, upper, step;
lower = -273.15;
upper = 0;
step = lower / -10; // Dividing lower temperature by given number
celcius = lower;
while (celcius <= upper)
farenheit = celcius * 9/5 + 32;
printf("%dt%dn", celcius, farenheit);
celcius = celcius + step;
But goes to absolutely random numbers when i try using float or double variables for more precise result: (There is code and output in terminal)
#include <stdio.h>
main()
float celcius, farenheit;
float lower, upper, step;
lower = -273.15;
upper = 0;
step = lower / -10; // Dividing lower temperature by given number
celcius = lower;
while (celcius <= upper)
farenheit = celcius * 9/5 + 32;
printf("%dt%dn", celcius, farenheit);
celcius = celcius + step;
Output:
1610612736 1073741824
1073741824 1073741824
-1073741824 1073741824
1073741824 536870912
-1073741824 536870912
1073741824 0
-2147483648 0
-2147483648 -2147483648
536870912 -1610612736
-2147483648 0
So what happened behind that number magic and how to get this to work?
c integer output floating negative-number
marked as duplicate by Lundin
StackExchange.ready(function()
if (StackExchange.options.isMobile) return;
$('.dupe-hammer-message-hover:not(.hover-bound)').each(function()
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');
$hover.hover(
function()
$hover.showInfoMessage('',
messageElement: $msg.clone().show(),
transient: false,
position: my: 'bottom left', at: 'top center', offsetTop: -7 ,
dismissable: false,
relativeToBody: true
);
,
function()
StackExchange.helpers.removeMessages();
);
);
);
Nov 12 '18 at 7:44
This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.
1
You should post the code that causes the erroneous output (it seems from your text as if the output is not from the code you posted)
– M.M
Nov 12 '18 at 3:00
regarding:main()
there are only two valid signatures formain
they areint main( void )
andint main( int argc, char * argv )
– user3629249
Nov 12 '18 at 3:04
regarding:lower = -273.15; upper = 0; step = lower / -10;
this is dividing a negative number by a negative number. The result will be positive
– user3629249
Nov 12 '18 at 3:06
please do not be editing the code in response to comments. Rather, add a 'EDIT' section that contains any edited code
– user3629249
Nov 12 '18 at 3:08
1
regarding:printf("%dt%dn", celcius, farenheit);
the variablescelcius
andfarenheit
arefloat
values. so theformat string
should be using%f
, not%d
– user3629249
Nov 12 '18 at 3:12
|
show 3 more comments
This question already has an answer here:
C program to convert Fahrenheit to Celsius always prints zero
8 answers
Hi, i am absolute newbie in programming. I am starting with learning C by book "C Programming Language (2nd Edition)" and stuck in very first example where we get exercise to write simple program that prints values of temperatures from lower to upper in 2 columns (tabs) that contains Celsius a Fahrenheit.
I'v get problem because trying to edit those code for:
- Celsius is main system.
- Steps measured dynamically by dividing lower on any given number.
And all work perfectly while i am using integers variables.
#include <stdio.h>
main()
int celcius, farenheit;
int lower, upper, step;
lower = -273.15;
upper = 0;
step = lower / -10; // Dividing lower temperature by given number
celcius = lower;
while (celcius <= upper)
farenheit = celcius * 9/5 + 32;
printf("%dt%dn", celcius, farenheit);
celcius = celcius + step;
But goes to absolutely random numbers when i try using float or double variables for more precise result: (There is code and output in terminal)
#include <stdio.h>
main()
float celcius, farenheit;
float lower, upper, step;
lower = -273.15;
upper = 0;
step = lower / -10; // Dividing lower temperature by given number
celcius = lower;
while (celcius <= upper)
farenheit = celcius * 9/5 + 32;
printf("%dt%dn", celcius, farenheit);
celcius = celcius + step;
Output:
1610612736 1073741824
1073741824 1073741824
-1073741824 1073741824
1073741824 536870912
-1073741824 536870912
1073741824 0
-2147483648 0
-2147483648 -2147483648
536870912 -1610612736
-2147483648 0
So what happened behind that number magic and how to get this to work?
c integer output floating negative-number
This question already has an answer here:
C program to convert Fahrenheit to Celsius always prints zero
8 answers
Hi, i am absolute newbie in programming. I am starting with learning C by book "C Programming Language (2nd Edition)" and stuck in very first example where we get exercise to write simple program that prints values of temperatures from lower to upper in 2 columns (tabs) that contains Celsius a Fahrenheit.
I'v get problem because trying to edit those code for:
- Celsius is main system.
- Steps measured dynamically by dividing lower on any given number.
And all work perfectly while i am using integers variables.
#include <stdio.h>
main()
int celcius, farenheit;
int lower, upper, step;
lower = -273.15;
upper = 0;
step = lower / -10; // Dividing lower temperature by given number
celcius = lower;
while (celcius <= upper)
farenheit = celcius * 9/5 + 32;
printf("%dt%dn", celcius, farenheit);
celcius = celcius + step;
But goes to absolutely random numbers when i try using float or double variables for more precise result: (There is code and output in terminal)
#include <stdio.h>
main()
float celcius, farenheit;
float lower, upper, step;
lower = -273.15;
upper = 0;
step = lower / -10; // Dividing lower temperature by given number
celcius = lower;
while (celcius <= upper)
farenheit = celcius * 9/5 + 32;
printf("%dt%dn", celcius, farenheit);
celcius = celcius + step;
Output:
1610612736 1073741824
1073741824 1073741824
-1073741824 1073741824
1073741824 536870912
-1073741824 536870912
1073741824 0
-2147483648 0
-2147483648 -2147483648
536870912 -1610612736
-2147483648 0
So what happened behind that number magic and how to get this to work?
This question already has an answer here:
C program to convert Fahrenheit to Celsius always prints zero
8 answers
c integer output floating negative-number
c integer output floating negative-number
edited Nov 12 '18 at 3:03
Ablix Corner
asked Nov 12 '18 at 2:58
Ablix CornerAblix Corner
32
32
marked as duplicate by Lundin
StackExchange.ready(function()
if (StackExchange.options.isMobile) return;
$('.dupe-hammer-message-hover:not(.hover-bound)').each(function()
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');
$hover.hover(
function()
$hover.showInfoMessage('',
messageElement: $msg.clone().show(),
transient: false,
position: my: 'bottom left', at: 'top center', offsetTop: -7 ,
dismissable: false,
relativeToBody: true
);
,
function()
StackExchange.helpers.removeMessages();
);
);
);
Nov 12 '18 at 7:44
This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.
marked as duplicate by Lundin
StackExchange.ready(function()
if (StackExchange.options.isMobile) return;
$('.dupe-hammer-message-hover:not(.hover-bound)').each(function()
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');
$hover.hover(
function()
$hover.showInfoMessage('',
messageElement: $msg.clone().show(),
transient: false,
position: my: 'bottom left', at: 'top center', offsetTop: -7 ,
dismissable: false,
relativeToBody: true
);
,
function()
StackExchange.helpers.removeMessages();
);
);
);
Nov 12 '18 at 7:44
This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.
1
You should post the code that causes the erroneous output (it seems from your text as if the output is not from the code you posted)
– M.M
Nov 12 '18 at 3:00
regarding:main()
there are only two valid signatures formain
they areint main( void )
andint main( int argc, char * argv )
– user3629249
Nov 12 '18 at 3:04
regarding:lower = -273.15; upper = 0; step = lower / -10;
this is dividing a negative number by a negative number. The result will be positive
– user3629249
Nov 12 '18 at 3:06
please do not be editing the code in response to comments. Rather, add a 'EDIT' section that contains any edited code
– user3629249
Nov 12 '18 at 3:08
1
regarding:printf("%dt%dn", celcius, farenheit);
the variablescelcius
andfarenheit
arefloat
values. so theformat string
should be using%f
, not%d
– user3629249
Nov 12 '18 at 3:12
|
show 3 more comments
1
You should post the code that causes the erroneous output (it seems from your text as if the output is not from the code you posted)
– M.M
Nov 12 '18 at 3:00
regarding:main()
there are only two valid signatures formain
they areint main( void )
andint main( int argc, char * argv )
– user3629249
Nov 12 '18 at 3:04
regarding:lower = -273.15; upper = 0; step = lower / -10;
this is dividing a negative number by a negative number. The result will be positive
– user3629249
Nov 12 '18 at 3:06
please do not be editing the code in response to comments. Rather, add a 'EDIT' section that contains any edited code
– user3629249
Nov 12 '18 at 3:08
1
regarding:printf("%dt%dn", celcius, farenheit);
the variablescelcius
andfarenheit
arefloat
values. so theformat string
should be using%f
, not%d
– user3629249
Nov 12 '18 at 3:12
1
1
You should post the code that causes the erroneous output (it seems from your text as if the output is not from the code you posted)
– M.M
Nov 12 '18 at 3:00
You should post the code that causes the erroneous output (it seems from your text as if the output is not from the code you posted)
– M.M
Nov 12 '18 at 3:00
regarding:
main()
there are only two valid signatures for main
they are int main( void )
and int main( int argc, char * argv )
– user3629249
Nov 12 '18 at 3:04
regarding:
main()
there are only two valid signatures for main
they are int main( void )
and int main( int argc, char * argv )
– user3629249
Nov 12 '18 at 3:04
regarding:
lower = -273.15; upper = 0; step = lower / -10;
this is dividing a negative number by a negative number. The result will be positive– user3629249
Nov 12 '18 at 3:06
regarding:
lower = -273.15; upper = 0; step = lower / -10;
this is dividing a negative number by a negative number. The result will be positive– user3629249
Nov 12 '18 at 3:06
please do not be editing the code in response to comments. Rather, add a 'EDIT' section that contains any edited code
– user3629249
Nov 12 '18 at 3:08
please do not be editing the code in response to comments. Rather, add a 'EDIT' section that contains any edited code
– user3629249
Nov 12 '18 at 3:08
1
1
regarding:
printf("%dt%dn", celcius, farenheit);
the variables celcius
and farenheit
are float
values. so the format string
should be using %f
, not %d
– user3629249
Nov 12 '18 at 3:12
regarding:
printf("%dt%dn", celcius, farenheit);
the variables celcius
and farenheit
are float
values. so the format string
should be using %f
, not %d
– user3629249
Nov 12 '18 at 3:12
|
show 3 more comments
2 Answers
2
active
oldest
votes
Two problems: first of all, you are doing integer division which causes your quotient to be truncated. Multiply in your calculations by 9./5., not 9/5. The former gives the actual result, but the latter performs integer division
Your second problem is using %d as your format specifier. You need %f which is for float
. Read the man pages for printf
for more details on this.
Thanks, i don't even knew what "%d" means, this fixed my problem, now i will read part of manual about format specifier's.
– Ablix Corner
Nov 12 '18 at 3:14
whencelcius
is afloat
, the codecelcius * 9/5 + 32
is correct, the associativity is(celcius * 9) / 5
etc.
– M.M
Nov 12 '18 at 3:38
add a comment |
hope this can help you
#include <stdio.h>
int main(int argc,char **argv)
double celcius, farenheit;
double lower, upper, step;
lower = -273.15;
upper = 0;
step = lower / -10; // Dividing lower temperature by given number
celcius = lower;
while (celcius <= upper)
farenheit = celcius * 9/5 + 32;
printf("%5.2ft%5.2fn", celcius, farenheit);
celcius = celcius + step;
return 0;
That also help, and giving result without lots of numbers after the comma.
– Ablix Corner
Nov 12 '18 at 3:18
2
it looks like your answer helped the OP, but consider adding some explanatory notes along with your code; now that the OP has replied, you can even address your notes to the OP's delight about "without a lot of numbers after the comma"; in other words, it sounds like the OP is not yet familiar with the options for using the%f
modifier inprintf()
; you can explain why you wrote that line the way you did, and link to useful documentation forprintf()
; TL; DR : please explain your code for the OP, who is just beginning with C
– landru27
Nov 12 '18 at 3:27
add a comment |
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
Two problems: first of all, you are doing integer division which causes your quotient to be truncated. Multiply in your calculations by 9./5., not 9/5. The former gives the actual result, but the latter performs integer division
Your second problem is using %d as your format specifier. You need %f which is for float
. Read the man pages for printf
for more details on this.
Thanks, i don't even knew what "%d" means, this fixed my problem, now i will read part of manual about format specifier's.
– Ablix Corner
Nov 12 '18 at 3:14
whencelcius
is afloat
, the codecelcius * 9/5 + 32
is correct, the associativity is(celcius * 9) / 5
etc.
– M.M
Nov 12 '18 at 3:38
add a comment |
Two problems: first of all, you are doing integer division which causes your quotient to be truncated. Multiply in your calculations by 9./5., not 9/5. The former gives the actual result, but the latter performs integer division
Your second problem is using %d as your format specifier. You need %f which is for float
. Read the man pages for printf
for more details on this.
Thanks, i don't even knew what "%d" means, this fixed my problem, now i will read part of manual about format specifier's.
– Ablix Corner
Nov 12 '18 at 3:14
whencelcius
is afloat
, the codecelcius * 9/5 + 32
is correct, the associativity is(celcius * 9) / 5
etc.
– M.M
Nov 12 '18 at 3:38
add a comment |
Two problems: first of all, you are doing integer division which causes your quotient to be truncated. Multiply in your calculations by 9./5., not 9/5. The former gives the actual result, but the latter performs integer division
Your second problem is using %d as your format specifier. You need %f which is for float
. Read the man pages for printf
for more details on this.
Two problems: first of all, you are doing integer division which causes your quotient to be truncated. Multiply in your calculations by 9./5., not 9/5. The former gives the actual result, but the latter performs integer division
Your second problem is using %d as your format specifier. You need %f which is for float
. Read the man pages for printf
for more details on this.
answered Nov 12 '18 at 3:06
stackptrstackptr
8,31023457
8,31023457
Thanks, i don't even knew what "%d" means, this fixed my problem, now i will read part of manual about format specifier's.
– Ablix Corner
Nov 12 '18 at 3:14
whencelcius
is afloat
, the codecelcius * 9/5 + 32
is correct, the associativity is(celcius * 9) / 5
etc.
– M.M
Nov 12 '18 at 3:38
add a comment |
Thanks, i don't even knew what "%d" means, this fixed my problem, now i will read part of manual about format specifier's.
– Ablix Corner
Nov 12 '18 at 3:14
whencelcius
is afloat
, the codecelcius * 9/5 + 32
is correct, the associativity is(celcius * 9) / 5
etc.
– M.M
Nov 12 '18 at 3:38
Thanks, i don't even knew what "%d" means, this fixed my problem, now i will read part of manual about format specifier's.
– Ablix Corner
Nov 12 '18 at 3:14
Thanks, i don't even knew what "%d" means, this fixed my problem, now i will read part of manual about format specifier's.
– Ablix Corner
Nov 12 '18 at 3:14
when
celcius
is a float
, the code celcius * 9/5 + 32
is correct, the associativity is (celcius * 9) / 5
etc.– M.M
Nov 12 '18 at 3:38
when
celcius
is a float
, the code celcius * 9/5 + 32
is correct, the associativity is (celcius * 9) / 5
etc.– M.M
Nov 12 '18 at 3:38
add a comment |
hope this can help you
#include <stdio.h>
int main(int argc,char **argv)
double celcius, farenheit;
double lower, upper, step;
lower = -273.15;
upper = 0;
step = lower / -10; // Dividing lower temperature by given number
celcius = lower;
while (celcius <= upper)
farenheit = celcius * 9/5 + 32;
printf("%5.2ft%5.2fn", celcius, farenheit);
celcius = celcius + step;
return 0;
That also help, and giving result without lots of numbers after the comma.
– Ablix Corner
Nov 12 '18 at 3:18
2
it looks like your answer helped the OP, but consider adding some explanatory notes along with your code; now that the OP has replied, you can even address your notes to the OP's delight about "without a lot of numbers after the comma"; in other words, it sounds like the OP is not yet familiar with the options for using the%f
modifier inprintf()
; you can explain why you wrote that line the way you did, and link to useful documentation forprintf()
; TL; DR : please explain your code for the OP, who is just beginning with C
– landru27
Nov 12 '18 at 3:27
add a comment |
hope this can help you
#include <stdio.h>
int main(int argc,char **argv)
double celcius, farenheit;
double lower, upper, step;
lower = -273.15;
upper = 0;
step = lower / -10; // Dividing lower temperature by given number
celcius = lower;
while (celcius <= upper)
farenheit = celcius * 9/5 + 32;
printf("%5.2ft%5.2fn", celcius, farenheit);
celcius = celcius + step;
return 0;
That also help, and giving result without lots of numbers after the comma.
– Ablix Corner
Nov 12 '18 at 3:18
2
it looks like your answer helped the OP, but consider adding some explanatory notes along with your code; now that the OP has replied, you can even address your notes to the OP's delight about "without a lot of numbers after the comma"; in other words, it sounds like the OP is not yet familiar with the options for using the%f
modifier inprintf()
; you can explain why you wrote that line the way you did, and link to useful documentation forprintf()
; TL; DR : please explain your code for the OP, who is just beginning with C
– landru27
Nov 12 '18 at 3:27
add a comment |
hope this can help you
#include <stdio.h>
int main(int argc,char **argv)
double celcius, farenheit;
double lower, upper, step;
lower = -273.15;
upper = 0;
step = lower / -10; // Dividing lower temperature by given number
celcius = lower;
while (celcius <= upper)
farenheit = celcius * 9/5 + 32;
printf("%5.2ft%5.2fn", celcius, farenheit);
celcius = celcius + step;
return 0;
hope this can help you
#include <stdio.h>
int main(int argc,char **argv)
double celcius, farenheit;
double lower, upper, step;
lower = -273.15;
upper = 0;
step = lower / -10; // Dividing lower temperature by given number
celcius = lower;
while (celcius <= upper)
farenheit = celcius * 9/5 + 32;
printf("%5.2ft%5.2fn", celcius, farenheit);
celcius = celcius + step;
return 0;
answered Nov 12 '18 at 3:14
lavenderwhalavenderwha
11
11
That also help, and giving result without lots of numbers after the comma.
– Ablix Corner
Nov 12 '18 at 3:18
2
it looks like your answer helped the OP, but consider adding some explanatory notes along with your code; now that the OP has replied, you can even address your notes to the OP's delight about "without a lot of numbers after the comma"; in other words, it sounds like the OP is not yet familiar with the options for using the%f
modifier inprintf()
; you can explain why you wrote that line the way you did, and link to useful documentation forprintf()
; TL; DR : please explain your code for the OP, who is just beginning with C
– landru27
Nov 12 '18 at 3:27
add a comment |
That also help, and giving result without lots of numbers after the comma.
– Ablix Corner
Nov 12 '18 at 3:18
2
it looks like your answer helped the OP, but consider adding some explanatory notes along with your code; now that the OP has replied, you can even address your notes to the OP's delight about "without a lot of numbers after the comma"; in other words, it sounds like the OP is not yet familiar with the options for using the%f
modifier inprintf()
; you can explain why you wrote that line the way you did, and link to useful documentation forprintf()
; TL; DR : please explain your code for the OP, who is just beginning with C
– landru27
Nov 12 '18 at 3:27
That also help, and giving result without lots of numbers after the comma.
– Ablix Corner
Nov 12 '18 at 3:18
That also help, and giving result without lots of numbers after the comma.
– Ablix Corner
Nov 12 '18 at 3:18
2
2
it looks like your answer helped the OP, but consider adding some explanatory notes along with your code; now that the OP has replied, you can even address your notes to the OP's delight about "without a lot of numbers after the comma"; in other words, it sounds like the OP is not yet familiar with the options for using the
%f
modifier in printf()
; you can explain why you wrote that line the way you did, and link to useful documentation for printf()
; TL; DR : please explain your code for the OP, who is just beginning with C– landru27
Nov 12 '18 at 3:27
it looks like your answer helped the OP, but consider adding some explanatory notes along with your code; now that the OP has replied, you can even address your notes to the OP's delight about "without a lot of numbers after the comma"; in other words, it sounds like the OP is not yet familiar with the options for using the
%f
modifier in printf()
; you can explain why you wrote that line the way you did, and link to useful documentation for printf()
; TL; DR : please explain your code for the OP, who is just beginning with C– landru27
Nov 12 '18 at 3:27
add a comment |
1
You should post the code that causes the erroneous output (it seems from your text as if the output is not from the code you posted)
– M.M
Nov 12 '18 at 3:00
regarding:
main()
there are only two valid signatures formain
they areint main( void )
andint main( int argc, char * argv )
– user3629249
Nov 12 '18 at 3:04
regarding:
lower = -273.15; upper = 0; step = lower / -10;
this is dividing a negative number by a negative number. The result will be positive– user3629249
Nov 12 '18 at 3:06
please do not be editing the code in response to comments. Rather, add a 'EDIT' section that contains any edited code
– user3629249
Nov 12 '18 at 3:08
1
regarding:
printf("%dt%dn", celcius, farenheit);
the variablescelcius
andfarenheit
arefloat
values. so theformat string
should be using%f
, not%d
– user3629249
Nov 12 '18 at 3:12