Re: [algogeeks] help with o/p why 0 comes???
The output of the below code is also zero , key reason is floating point number are stored as *mantissa, exponent. * float dec=1.00; printf("\n dec=%d , float =%f",dec,dec); On Fri, Mar 1, 2013 at 12:05 PM, Karthikeyan V.B wrote: > O/p will not be 0. > > 1.00 is the result which when read as %d takes the decimal value of > stored in memory - it will not be 1.00 or 0. > > Since float is not stored as direct binary in memory as integer is stored, > instead there's a separate procedure for storing float as binary in memory > > -- > You received this message because you are subscribed to the Google Groups > "Algorithm Geeks" group. > To unsubscribe from this group and stop receiving emails from it, send an > email to algogeeks+unsubscr...@googlegroups.com. > For more options, visit https://groups.google.com/groups/opt_out. > > > -- You received this message because you are subscribed to the Google Groups "Algorithm Geeks" group. To unsubscribe from this group and stop receiving emails from it, send an email to algogeeks+unsubscr...@googlegroups.com. For more options, visit https://groups.google.com/groups/opt_out.
Re: [algogeeks] help with o/p why 0 comes???
Hi Shubham, This may be because your final data type you have chosen for your output is float, n the one which you are trying to print is of int data type (i.e. %d). Suggestion : Try changing %d to %f and see whether it works for you or not? Thanks & Regards, Pratts On 01-Mar-2013, at 1:11, Shubham Sandeep wrote: > code snippet: > int main() > { > printf ("%d\n",(float)((int)(3.5/2))); > return 0; > } > > -- > Regards, > SHUBHAM SANDEEP > IT 3rd yr. > NIT ALD. > -- > You received this message because you are subscribed to the Google Groups > "Algorithm Geeks" group. > To unsubscribe from this group and stop receiving emails from it, send an > email to algogeeks+unsubscr...@googlegroups.com. > For more options, visit https://groups.google.com/groups/opt_out. > > -- You received this message because you are subscribed to the Google Groups "Algorithm Geeks" group. To unsubscribe from this group and stop receiving emails from it, send an email to algogeeks+unsubscr...@googlegroups.com. For more options, visit https://groups.google.com/groups/opt_out.
Re: [algogeeks] help with o/p why 0 comes???
Ya, its looking like the problem of 'i686-apple-darwin11-llvm-gcc-4.2'. For me as well it shows different outputs. On Mon, Mar 4, 2013 at 4:45 PM, rohit jangid wrote: > yup , it is showing > 0 > 0 > > on ideone as well . so my gcc compiler > is i686-apple-darwin11-llvm-gcc-4.2. that can be the reason . from here it > appears 0 is just a coincidence and it depends on compiler implementation . > C doesn't define any such behavior. > > > On Mon, Mar 4, 2013 at 3:45 PM, Shubham Sandeep < > s.shubhamsand...@gmail.com> wrote: > >> on my system every time o/p is 0 >> using ubuntu 10.04 ,gcc compiler >> >> >> On Mon, Mar 4, 2013 at 7:34 AM, rohit jangid wrote: >> >>> output for me for the previous snippet >>> >>> localhost:slingshot rohitjangid$ ./a.out >>> 1799476872 >>> 1799474584 >>> localhost:slingshot rohitjangid$ ./a.out >>> 1710327432 >>> 1710325144 >>> localhost:slingshot rohitjangid$ ./a.out >>> 1856128648 >>> 1856126360 >>> localhost:slingshot rohitjangid$ ./a.out >>> 1724065416 >>> 1724063128 >>> >>> >>> On Mon, Mar 4, 2013 at 7:33 AM, rohit jangid wrote: >>> yeah true . one interesting thing I noticed is that if you run this code #include int main() { int i = 0; do { printf ("%d\n",(float)1); }while(i++ < 1); return 0; } one would expect same output in both the rows but surprisingly it came different for me every time . any clues .. why ? On Fri, Mar 1, 2013 at 12:05 PM, Karthikeyan V.B wrote: > O/p will not be 0. > > 1.00 is the result which when read as %d takes the decimal value > of stored in memory - it will not be 1.00 or 0. > > Since float is not stored as direct binary in memory as integer is > stored, instead there's a separate procedure for storing float as binary > in > memory > > -- > You received this message because you are subscribed to the Google > Groups "Algorithm Geeks" group. > To unsubscribe from this group and stop receiving emails from it, send > an email to algogeeks+unsubscr...@googlegroups.com. > For more options, visit https://groups.google.com/groups/opt_out. > > > -- Rohit Jangid Graduate Deptt. of Computer Engineering NSIT, Delhi University, India >>> >>> >>> -- >>> Rohit Jangid >>> Graduate >>> Deptt. of Computer Engineering >>> NSIT, Delhi University, India >>> >>> -- >>> You received this message because you are subscribed to the Google >>> Groups "Algorithm Geeks" group. >>> To unsubscribe from this group and stop receiving emails from it, send >>> an email to algogeeks+unsubscr...@googlegroups.com. >>> For more options, visit https://groups.google.com/groups/opt_out. >>> >>> >>> >> >> >> >> -- >> Regards, >> SHUBHAM SANDEEP >> IT 3rd yr. >> NIT ALD. >> >> -- >> You received this message because you are subscribed to the Google Groups >> "Algorithm Geeks" group. >> To unsubscribe from this group and stop receiving emails from it, send an >> email to algogeeks+unsubscr...@googlegroups.com. >> For more options, visit https://groups.google.com/groups/opt_out. >> >> >> > > > > -- > Rohit Jangid > Graduate > Deptt. of Computer Engineering > NSIT, Delhi University, India > > -- > You received this message because you are subscribed to the Google Groups > "Algorithm Geeks" group. > To unsubscribe from this group and stop receiving emails from it, send an > email to algogeeks+unsubscr...@googlegroups.com. > For more options, visit https://groups.google.com/groups/opt_out. > > > -- You received this message because you are subscribed to the Google Groups "Algorithm Geeks" group. To unsubscribe from this group and stop receiving emails from it, send an email to algogeeks+unsubscr...@googlegroups.com. For more options, visit https://groups.google.com/groups/opt_out.
Re: [algogeeks] help with o/p why 0 comes???
yup , it is showing 0 0 on ideone as well . so my gcc compiler is i686-apple-darwin11-llvm-gcc-4.2. that can be the reason . from here it appears 0 is just a coincidence and it depends on compiler implementation . C doesn't define any such behavior. On Mon, Mar 4, 2013 at 3:45 PM, Shubham Sandeep wrote: > on my system every time o/p is 0 > using ubuntu 10.04 ,gcc compiler > > > On Mon, Mar 4, 2013 at 7:34 AM, rohit jangid wrote: > >> output for me for the previous snippet >> >> localhost:slingshot rohitjangid$ ./a.out >> 1799476872 >> 1799474584 >> localhost:slingshot rohitjangid$ ./a.out >> 1710327432 >> 1710325144 >> localhost:slingshot rohitjangid$ ./a.out >> 1856128648 >> 1856126360 >> localhost:slingshot rohitjangid$ ./a.out >> 1724065416 >> 1724063128 >> >> >> On Mon, Mar 4, 2013 at 7:33 AM, rohit jangid wrote: >> >>> yeah true . one interesting thing I noticed is that if you run this code >>> >>> #include >>> int main() >>> { >>> int i = 0; >>> do { >>> printf ("%d\n",(float)1); >>> }while(i++ < 1); >>> return 0; >>> } >>> >>> one would expect same output in both the rows but surprisingly it came >>> different for me every time . >>> any clues .. why ? >>> >>> >>> >>> >>> On Fri, Mar 1, 2013 at 12:05 PM, Karthikeyan V.B wrote: >>> O/p will not be 0. 1.00 is the result which when read as %d takes the decimal value of stored in memory - it will not be 1.00 or 0. Since float is not stored as direct binary in memory as integer is stored, instead there's a separate procedure for storing float as binary in memory -- You received this message because you are subscribed to the Google Groups "Algorithm Geeks" group. To unsubscribe from this group and stop receiving emails from it, send an email to algogeeks+unsubscr...@googlegroups.com. For more options, visit https://groups.google.com/groups/opt_out. >>> >>> >>> >>> -- >>> Rohit Jangid >>> Graduate >>> Deptt. of Computer Engineering >>> NSIT, Delhi University, India >>> >>> >> >> >> -- >> Rohit Jangid >> Graduate >> Deptt. of Computer Engineering >> NSIT, Delhi University, India >> >> -- >> You received this message because you are subscribed to the Google Groups >> "Algorithm Geeks" group. >> To unsubscribe from this group and stop receiving emails from it, send an >> email to algogeeks+unsubscr...@googlegroups.com. >> For more options, visit https://groups.google.com/groups/opt_out. >> >> >> > > > > -- > Regards, > SHUBHAM SANDEEP > IT 3rd yr. > NIT ALD. > > -- > You received this message because you are subscribed to the Google Groups > "Algorithm Geeks" group. > To unsubscribe from this group and stop receiving emails from it, send an > email to algogeeks+unsubscr...@googlegroups.com. > For more options, visit https://groups.google.com/groups/opt_out. > > > -- Rohit Jangid Graduate Deptt. of Computer Engineering NSIT, Delhi University, India -- You received this message because you are subscribed to the Google Groups "Algorithm Geeks" group. To unsubscribe from this group and stop receiving emails from it, send an email to algogeeks+unsubscr...@googlegroups.com. For more options, visit https://groups.google.com/groups/opt_out.
Re: [algogeeks] help with o/p why 0 comes???
on my system every time o/p is 0 using ubuntu 10.04 ,gcc compiler On Mon, Mar 4, 2013 at 7:34 AM, rohit jangid wrote: > output for me for the previous snippet > > localhost:slingshot rohitjangid$ ./a.out > 1799476872 > 1799474584 > localhost:slingshot rohitjangid$ ./a.out > 1710327432 > 1710325144 > localhost:slingshot rohitjangid$ ./a.out > 1856128648 > 1856126360 > localhost:slingshot rohitjangid$ ./a.out > 1724065416 > 1724063128 > > > On Mon, Mar 4, 2013 at 7:33 AM, rohit jangid wrote: > >> yeah true . one interesting thing I noticed is that if you run this code >> >> #include >> int main() >> { >> int i = 0; >> do { >> printf ("%d\n",(float)1); >> }while(i++ < 1); >> return 0; >> } >> >> one would expect same output in both the rows but surprisingly it came >> different for me every time . >> any clues .. why ? >> >> >> >> >> On Fri, Mar 1, 2013 at 12:05 PM, Karthikeyan V.B wrote: >> >>> O/p will not be 0. >>> >>> 1.00 is the result which when read as %d takes the decimal value of >>> stored in memory - it will not be 1.00 or 0. >>> >>> Since float is not stored as direct binary in memory as integer is >>> stored, instead there's a separate procedure for storing float as binary in >>> memory >>> >>> -- >>> You received this message because you are subscribed to the Google >>> Groups "Algorithm Geeks" group. >>> To unsubscribe from this group and stop receiving emails from it, send >>> an email to algogeeks+unsubscr...@googlegroups.com. >>> For more options, visit https://groups.google.com/groups/opt_out. >>> >>> >>> >> >> >> >> -- >> Rohit Jangid >> Graduate >> Deptt. of Computer Engineering >> NSIT, Delhi University, India >> >> > > > -- > Rohit Jangid > Graduate > Deptt. of Computer Engineering > NSIT, Delhi University, India > > -- > You received this message because you are subscribed to the Google Groups > "Algorithm Geeks" group. > To unsubscribe from this group and stop receiving emails from it, send an > email to algogeeks+unsubscr...@googlegroups.com. > For more options, visit https://groups.google.com/groups/opt_out. > > > -- Regards, SHUBHAM SANDEEP IT 3rd yr. NIT ALD. -- You received this message because you are subscribed to the Google Groups "Algorithm Geeks" group. To unsubscribe from this group and stop receiving emails from it, send an email to algogeeks+unsubscr...@googlegroups.com. For more options, visit https://groups.google.com/groups/opt_out.
Re: [algogeeks] help with o/p why 0 comes???
output for me for the previous snippet localhost:slingshot rohitjangid$ ./a.out 1799476872 1799474584 localhost:slingshot rohitjangid$ ./a.out 1710327432 1710325144 localhost:slingshot rohitjangid$ ./a.out 1856128648 1856126360 localhost:slingshot rohitjangid$ ./a.out 1724065416 1724063128 On Mon, Mar 4, 2013 at 7:33 AM, rohit jangid wrote: > yeah true . one interesting thing I noticed is that if you run this code > > #include > int main() > { > int i = 0; > do { > printf ("%d\n",(float)1); > }while(i++ < 1); > return 0; > } > > one would expect same output in both the rows but surprisingly it came > different for me every time . > any clues .. why ? > > > > > On Fri, Mar 1, 2013 at 12:05 PM, Karthikeyan V.B wrote: > >> O/p will not be 0. >> >> 1.00 is the result which when read as %d takes the decimal value of >> stored in memory - it will not be 1.00 or 0. >> >> Since float is not stored as direct binary in memory as integer is >> stored, instead there's a separate procedure for storing float as binary in >> memory >> >> -- >> You received this message because you are subscribed to the Google Groups >> "Algorithm Geeks" group. >> To unsubscribe from this group and stop receiving emails from it, send an >> email to algogeeks+unsubscr...@googlegroups.com. >> For more options, visit https://groups.google.com/groups/opt_out. >> >> >> > > > > -- > Rohit Jangid > Graduate > Deptt. of Computer Engineering > NSIT, Delhi University, India > > -- Rohit Jangid Graduate Deptt. of Computer Engineering NSIT, Delhi University, India -- You received this message because you are subscribed to the Google Groups "Algorithm Geeks" group. To unsubscribe from this group and stop receiving emails from it, send an email to algogeeks+unsubscr...@googlegroups.com. For more options, visit https://groups.google.com/groups/opt_out.
Re: [algogeeks] help with o/p why 0 comes???
yeah true . one interesting thing I noticed is that if you run this code #include int main() { int i = 0; do { printf ("%d\n",(float)1); }while(i++ < 1); return 0; } one would expect same output in both the rows but surprisingly it came different for me every time . any clues .. why ? On Fri, Mar 1, 2013 at 12:05 PM, Karthikeyan V.B wrote: > O/p will not be 0. > > 1.00 is the result which when read as %d takes the decimal value of > stored in memory - it will not be 1.00 or 0. > > Since float is not stored as direct binary in memory as integer is stored, > instead there's a separate procedure for storing float as binary in memory > > -- > You received this message because you are subscribed to the Google Groups > "Algorithm Geeks" group. > To unsubscribe from this group and stop receiving emails from it, send an > email to algogeeks+unsubscr...@googlegroups.com. > For more options, visit https://groups.google.com/groups/opt_out. > > > -- Rohit Jangid Graduate Deptt. of Computer Engineering NSIT, Delhi University, India -- You received this message because you are subscribed to the Google Groups "Algorithm Geeks" group. To unsubscribe from this group and stop receiving emails from it, send an email to algogeeks+unsubscr...@googlegroups.com. For more options, visit https://groups.google.com/groups/opt_out.
Re: [algogeeks] help with o/p why 0 comes???
O/p will not be 0. 1.00 is the result which when read as %d takes the decimal value of stored in memory - it will not be 1.00 or 0. Since float is not stored as direct binary in memory as integer is stored, instead there's a separate procedure for storing float as binary in memory -- You received this message because you are subscribed to the Google Groups "Algorithm Geeks" group. To unsubscribe from this group and stop receiving emails from it, send an email to algogeeks+unsubscr...@googlegroups.com. For more options, visit https://groups.google.com/groups/opt_out.
Re: [algogeeks] help with o/p why 0 comes???
thank you for pointing out that format was the key point. On Fri, Mar 1, 2013 at 1:19 AM, gmagog...@gmail.com wrote: > I think this is because of type mismatch. You are enforcing your program > to read a floating point number in the way of reading a integer. And they > have totally different format. If you have -Wall turned on, you should see > a warning. > > Yanan Cao > > > On Thu, Feb 28, 2013 at 1:41 PM, Shubham Sandeep < > s.shubhamsand...@gmail.com> wrote: > >> code snippet: >> *int main() >> { >> printf ("%d\n",(float)((int)(3.5/2))); >> return 0; >> }* >> >> >> -- >> Regards, >> SHUBHAM SANDEEP >> IT 3rd yr. >> NIT ALD. >> >> -- >> You received this message because you are subscribed to the Google Groups >> "Algorithm Geeks" group. >> To unsubscribe from this group and stop receiving emails from it, send an >> email to algogeeks+unsubscr...@googlegroups.com. >> For more options, visit https://groups.google.com/groups/opt_out. >> >> >> > > -- > You received this message because you are subscribed to the Google Groups > "Algorithm Geeks" group. > To unsubscribe from this group and stop receiving emails from it, send an > email to algogeeks+unsubscr...@googlegroups.com. > For more options, visit https://groups.google.com/groups/opt_out. > > > -- Regards, SHUBHAM SANDEEP IT 3rd yr. NIT ALD. -- You received this message because you are subscribed to the Google Groups "Algorithm Geeks" group. To unsubscribe from this group and stop receiving emails from it, send an email to algogeeks+unsubscr...@googlegroups.com. For more options, visit https://groups.google.com/groups/opt_out.
Re: [algogeeks] help with o/p why 0 comes???
I think this is because of type mismatch. You are enforcing your program to read a floating point number in the way of reading a integer. And they have totally different format. If you have -Wall turned on, you should see a warning. Yanan Cao On Thu, Feb 28, 2013 at 1:41 PM, Shubham Sandeep wrote: > code snippet: > *int main() > { > printf ("%d\n",(float)((int)(3.5/2))); > return 0; > }* > > -- > Regards, > SHUBHAM SANDEEP > IT 3rd yr. > NIT ALD. > > -- > You received this message because you are subscribed to the Google Groups > "Algorithm Geeks" group. > To unsubscribe from this group and stop receiving emails from it, send an > email to algogeeks+unsubscr...@googlegroups.com. > For more options, visit https://groups.google.com/groups/opt_out. > > > -- You received this message because you are subscribed to the Google Groups "Algorithm Geeks" group. To unsubscribe from this group and stop receiving emails from it, send an email to algogeeks+unsubscr...@googlegroups.com. For more options, visit https://groups.google.com/groups/opt_out.
[algogeeks] help with o/p why 0 comes???
code snippet: *int main() { printf ("%d\n",(float)((int)(3.5/2))); return 0; }* -- Regards, SHUBHAM SANDEEP IT 3rd yr. NIT ALD. -- You received this message because you are subscribed to the Google Groups "Algorithm Geeks" group. To unsubscribe from this group and stop receiving emails from it, send an email to algogeeks+unsubscr...@googlegroups.com. For more options, visit https://groups.google.com/groups/opt_out.