* float @ 2010-01-08 16:30 phoenixxz 2010-01-08 4:42 ` float Bryan Christ ` (5 more replies) 0 siblings, 6 replies; 20+ messages in thread From: phoenixxz @ 2010-01-08 16:30 UTC (permalink / raw) To: linux-c-programming float i=1.34; printf("%f",i); ->> 0 why? ^ permalink raw reply [flat|nested] 20+ messages in thread
* Re: float 2010-01-08 16:30 float phoenixxz @ 2010-01-08 4:42 ` Bryan Christ 2010-01-08 16:45 ` float Fawad Lateef 2010-01-08 16:40 ` float Nanakos Chrysostomos ` (4 subsequent siblings) 5 siblings, 1 reply; 20+ messages in thread From: Bryan Christ @ 2010-01-08 4:42 UTC (permalink / raw) To: phoenixxz; +Cc: linux-c-programming try float i=1.34F On Fri, 2010-01-08 at 16:30 +0000, phoenixxz wrote: > float i=1.34; > printf("%f",i); > > > ->> 0 > why? > -- > To unsubscribe from this list: send the line "unsubscribe linux-c-programming" in > the body of a message to majordomo@vger.kernel.org > More majordomo info at http://vger.kernel.org/majordomo-info.html ^ permalink raw reply [flat|nested] 20+ messages in thread
* Re: float 2010-01-08 4:42 ` float Bryan Christ @ 2010-01-08 16:45 ` Fawad Lateef 0 siblings, 0 replies; 20+ messages in thread From: Fawad Lateef @ 2010-01-08 16:45 UTC (permalink / raw) To: bryan.christ; +Cc: phoenixxz, linux-c-programming On Fri, Jan 8, 2010 at 4:42 AM, Bryan Christ <bryan.christ@hp.com> wrote: > try > > float i=1.34F On linux (gcc compiler) it will work without 'F' too. > > On Fri, 2010-01-08 at 16:30 +0000, phoenixxz wrote: >> float i=1.34; >> printf("%f",i); >> >> >> ->> 0 >> why? >> -- >> To unsubscribe from this list: send the line "unsubscribe linux-c-programming" in >> the body of a message to majordomo@vger.kernel.org >> More majordomo info at http://vger.kernel.org/majordomo-info.html > > > -- > To unsubscribe from this list: send the line "unsubscribe linux-c-programming" in > the body of a message to majordomo@vger.kernel.org > More majordomo info at http://vger.kernel.org/majordomo-info.html > -- Fawad Lateef -- To unsubscribe from this list: send the line "unsubscribe linux-c-programming" in the body of a message to majordomo@vger.kernel.org More majordomo info at http://vger.kernel.org/majordomo-info.html ^ permalink raw reply [flat|nested] 20+ messages in thread
* Re: float 2010-01-08 16:30 float phoenixxz 2010-01-08 4:42 ` float Bryan Christ @ 2010-01-08 16:40 ` Nanakos Chrysostomos 2010-01-08 16:44 ` float Fawad Lateef ` (3 subsequent siblings) 5 siblings, 0 replies; 20+ messages in thread From: Nanakos Chrysostomos @ 2010-01-08 16:40 UTC (permalink / raw) To: phoenixxz; +Cc: linux-c-programming On 8/1/2010 18:30, phoenixxz wrote: > float i=1.34; > printf("%f",i); > > > ->> 0 > why? > -- > To unsubscribe from this list: send the line "unsubscribe linux-c-programming" in > the body of a message to majordomo@vger.kernel.org > More majordomo info at http://vger.kernel.org/majordomo-info.html > printf("%lf",i); ^ permalink raw reply [flat|nested] 20+ messages in thread
* Re: float 2010-01-08 16:30 float phoenixxz 2010-01-08 4:42 ` float Bryan Christ 2010-01-08 16:40 ` float Nanakos Chrysostomos @ 2010-01-08 16:44 ` Fawad Lateef 2010-01-08 16:47 ` float Nanakos Chrysostomos ` (2 subsequent siblings) 5 siblings, 0 replies; 20+ messages in thread From: Fawad Lateef @ 2010-01-08 16:44 UTC (permalink / raw) To: phoenixxz; +Cc: linux-c-programming Hello, On Fri, Jan 8, 2010 at 4:30 PM, phoenixxz <vim.xxz@gmail.com> wrote: > > float i=1.34; > printf("%f",i); > > > ->> 0 > why? > -- This should be fine. You might be doing some mistake. Can you give some more details about what you want to do ? -- Fawad Lateef ^ permalink raw reply [flat|nested] 20+ messages in thread
* Re: float 2010-01-08 16:30 float phoenixxz ` (2 preceding siblings ...) 2010-01-08 16:44 ` float Fawad Lateef @ 2010-01-08 16:47 ` Nanakos Chrysostomos 2010-01-08 17:11 ` float Michal Nazarewicz 2010-01-08 18:00 ` float andrej.gelenberg 2010-01-08 22:05 ` float Glynn Clements 5 siblings, 1 reply; 20+ messages in thread From: Nanakos Chrysostomos @ 2010-01-08 16:47 UTC (permalink / raw) To: phoenixxz; +Cc: linux-c-programming On 8/1/2010 18:30, phoenixxz wrote: > float i=1.34; > printf("%f",i); > > > ->> 0 > why? > -- > To unsubscribe from this list: send the line "unsubscribe linux-c-programming" in > the body of a message to majordomo@vger.kernel.org > More majordomo info at http://vger.kernel.org/majordomo-info.html > printf("%lf",i); ^ permalink raw reply [flat|nested] 20+ messages in thread
* Re: float 2010-01-08 16:47 ` float Nanakos Chrysostomos @ 2010-01-08 17:11 ` Michal Nazarewicz 2010-01-08 17:19 ` float Nanakos Chrysostomos 2010-01-08 17:21 ` float Nanakos Chrysostomos 0 siblings, 2 replies; 20+ messages in thread From: Michal Nazarewicz @ 2010-01-08 17:11 UTC (permalink / raw) To: Nanakos Chrysostomos; +Cc: phoenixxz, linux-c-programming > On 8/1/2010 18:30, phoenixxz wrote: >> float i=1.34; >> printf("%f",i); >> >> ->> 0 >> why? Nanakos Chrysostomos <linuxcprog@wired-net.gr> writes: > printf("%lf",i); %lf is actually invalid format (though glibc will gladly treat it the same way it treats %f). When passing a float value to a function with variable number of arguments it is promoted to double. -- Best regards, _ _ .o. | Liege of Serenly Enlightened Majesty of o' \,=./ `o ..o | Computer Science, Michal "mina86" Nazarewicz (o o) ooo +--<mina86*tlen.pl>--<jid:mina86*jabber.org>--ooO--(_)--Ooo-- ^ permalink raw reply [flat|nested] 20+ messages in thread
* Re: float 2010-01-08 17:11 ` float Michal Nazarewicz @ 2010-01-08 17:19 ` Nanakos Chrysostomos 2010-01-08 17:21 ` float Nanakos Chrysostomos 1 sibling, 0 replies; 20+ messages in thread From: Nanakos Chrysostomos @ 2010-01-08 17:19 UTC (permalink / raw) To: Michal Nazarewicz; +Cc: phoenixxz, linux-c-programming On 8/1/2010 19:11, Michal Nazarewicz wrote: >> On 8/1/2010 18:30, phoenixxz wrote: >> >>> float i=1.34; >>> printf("%f",i); >>> >>> ->> 0 >>> why? >>> > Nanakos Chrysostomos <linuxcprog@wired-net.gr> writes: > >> printf("%lf",i); >> > %lf is actually invalid format (though glibc will gladly treat it the > same way it treats %f). When passing a float value to a function with > variable number of arguments it is promoted to double. > > It could then be: printf("%lf", (double)i); ^ permalink raw reply [flat|nested] 20+ messages in thread
* Re: float 2010-01-08 17:11 ` float Michal Nazarewicz 2010-01-08 17:19 ` float Nanakos Chrysostomos @ 2010-01-08 17:21 ` Nanakos Chrysostomos 2010-01-08 17:43 ` float Michal Nazarewicz 1 sibling, 1 reply; 20+ messages in thread From: Nanakos Chrysostomos @ 2010-01-08 17:21 UTC (permalink / raw) To: Michal Nazarewicz; +Cc: phoenixxz, linux-c-programming On 8/1/2010 19:11, Michal Nazarewicz wrote: >> On 8/1/2010 18:30, phoenixxz wrote: >> >>> float i=1.34; >>> printf("%f",i); >>> >>> ->> 0 >>> why? >>> > Nanakos Chrysostomos <linuxcprog@wired-net.gr> writes: > >> printf("%lf",i); >> > %lf is actually invalid format (though glibc will gladly treat it the > same way it treats %f). When passing a float value to a function with > variable number of arguments it is promoted to double. > > Sorry for the previous one... it could be then. printf("%f",(double)i); ^ permalink raw reply [flat|nested] 20+ messages in thread
* Re: float 2010-01-08 17:21 ` float Nanakos Chrysostomos @ 2010-01-08 17:43 ` Michal Nazarewicz 0 siblings, 0 replies; 20+ messages in thread From: Michal Nazarewicz @ 2010-01-08 17:43 UTC (permalink / raw) To: Nanakos Chrysostomos; +Cc: phoenixxz, linux-c-programming > On 8/1/2010 19:11, Michal Nazarewicz wrote: >> %lf is actually invalid format (though glibc will gladly treat it the >> same way it treats %f). When passing a float value to a function with >> variable number of arguments it is promoted to double. Nanakos Chrysostomos <linuxcprog@wired-net.gr> writes: > it could be then. > > printf("%f",(double)i); Expect explicit cast is not required here as (in case of "..." arguments) it is done implicitly as I described. -- Best regards, _ _ .o. | Liege of Serenly Enlightened Majesty of o' \,=./ `o ..o | Computer Science, Michal "mina86" Nazarewicz (o o) ooo +--<mina86*tlen.pl>--<jid:mina86*jabber.org>--ooO--(_)--Ooo-- ^ permalink raw reply [flat|nested] 20+ messages in thread
* Re: float 2010-01-08 16:30 float phoenixxz ` (3 preceding siblings ...) 2010-01-08 16:47 ` float Nanakos Chrysostomos @ 2010-01-08 18:00 ` andrej.gelenberg 2010-01-08 22:05 ` float Glynn Clements 5 siblings, 0 replies; 20+ messages in thread From: andrej.gelenberg @ 2010-01-08 18:00 UTC (permalink / raw) To: phoenixxz; +Cc: linux-c-programming Hi, work for me. cat >test.c <<END #include <stdio.h> int main(){ float i=1.34; printf("%f\n",i); return 0; } END gcc -o test test.c && ./test output: 1.340000 phoenixxz writes: > float i=1.34; > printf("%f",i); > > > ->> 0 > why? > -- > To unsubscribe from this list: send the line "unsubscribe linux-c-programming" in > the body of a message to majordomo@vger.kernel.org > More majordomo info at http://vger.kernel.org/majordomo-info.html ^ permalink raw reply [flat|nested] 20+ messages in thread
* Re: float 2010-01-08 16:30 float phoenixxz ` (4 preceding siblings ...) 2010-01-08 18:00 ` float andrej.gelenberg @ 2010-01-08 22:05 ` Glynn Clements 5 siblings, 0 replies; 20+ messages in thread From: Glynn Clements @ 2010-01-08 22:05 UTC (permalink / raw) To: phoenixxz; +Cc: linux-c-programming phoenixxz wrote: > float i=1.34; > printf("%f",i); > > > ->> 0 > why? You need to include <stdio.h> and write a main() function ;) Beyond that: stdout will be line-buffered (if it's associated with a terminal, block-buffered otherwise), so you should add a "\n" to the end of the format string. The lack of line buffering might have an effect if the program terminates abnormally (exit() or returning from main() should cause buffered streams to be flushed). A working example: $ cat test.c #include <stdio.h> int main(void) { float i=1.34; printf("%f\n",i); return 0; } $ gcc test.c $ ./a.out 1.340000 -- Glynn Clements <glynn@gclements.plus.com> ^ permalink raw reply [flat|nested] 20+ messages in thread
* float @ 2011-01-11 7:38 ratheesh k 2011-01-11 7:46 ` float Thomas Weber ` (2 more replies) 0 siblings, 3 replies; 20+ messages in thread From: ratheesh k @ 2011-01-11 7:38 UTC (permalink / raw) To: linux-c-programming I could not understand why it getting printed like this. Could any body tell me. #include <stdio.h> int main() { float f=0.0f; int i; for(i=0;i<10;i++) f = f + 0.1f; if(f == 1.0f) printf("f is 1.0 \n"); else printf("f is NOT 1.0\n"); return 0; } ^ permalink raw reply [flat|nested] 20+ messages in thread
* Re: float 2011-01-11 7:38 float ratheesh k @ 2011-01-11 7:46 ` Thomas Weber 2011-01-11 7:56 ` float Amit Virdi 2011-01-11 7:55 ` float Uriel Corfa 2011-01-11 8:38 ` float Andrej Gelenberg 2 siblings, 1 reply; 20+ messages in thread From: Thomas Weber @ 2011-01-11 7:46 UTC (permalink / raw) To: ratheesh k; +Cc: linux-c-programming Am 11.01.2011 08:38, schrieb ratheesh k: > I could not understand why it getting printed like this. Could any > body tell me. > > #include <stdio.h> > > int main() > { > float f=0.0f; > int i; > > for(i=0;i<10;i++) missing braces here? > f = f + 0.1f; > > if(f == 1.0f) > printf("f is 1.0 \n"); > else > printf("f is NOT 1.0\n"); > > return 0; > } > -- Thomas ^ permalink raw reply [flat|nested] 20+ messages in thread
* Re: float 2011-01-11 7:46 ` float Thomas Weber @ 2011-01-11 7:56 ` Amit Virdi 0 siblings, 0 replies; 20+ messages in thread From: Amit Virdi @ 2011-01-11 7:56 UTC (permalink / raw) To: Thomas Weber; +Cc: ratheesh k, linux-c-programming In floating point comparisons, test for range rather testing for equality. Something like: int compare_float(float f1, float f2) { float precision = 0.00001; if (((f1 - precision) < f2) && ((f1 + precision) > f2)) { return 1; } else { return 0; } } This is because, floating point numbers are stored in binary format and rounds off in strange ways. On 01/11/2011 01:16 PM, Thomas Weber wrote: > Am 11.01.2011 08:38, schrieb ratheesh k: >> I could not understand why it getting printed like this. Could any >> body tell me. >> >> #include <stdio.h> >> >> int main() >> { >> float f=0.0f; >> int i; >> >> for(i=0;i<10;i++) > missing braces here? >> f = f + 0.1f; >> >> if(f == 1.0f) >> printf("f is 1.0 \n"); >> else >> printf("f is NOT 1.0\n"); >> >> return 0; >> } >> -- > > > Thomas > > -- > To unsubscribe from this list: send the line "unsubscribe linux-c-programming" in > the body of a message to majordomo@vger.kernel.org > More majordomo info at http://vger.kernel.org/majordomo-info.html > -- ~Amit Virdi ^ permalink raw reply [flat|nested] 20+ messages in thread
* Re: float 2011-01-11 7:38 float ratheesh k 2011-01-11 7:46 ` float Thomas Weber @ 2011-01-11 7:55 ` Uriel Corfa 2011-01-11 8:01 ` float Thomas Weber 2011-01-11 8:38 ` float Andrej Gelenberg 2 siblings, 1 reply; 20+ messages in thread From: Uriel Corfa @ 2011-01-11 7:55 UTC (permalink / raw) To: ratheesh k; +Cc: linux-c-programming I'd say : rounding errors. Floating point goes in mysterious ways. On Tue, Jan 11, 2011 at 8:38 AM, ratheesh k <ratheesh.ksz@gmail.com> wrote: > I could not understand why it getting printed like this. Could any > body tell me. > > #include <stdio.h> > > int main() > { > float f=0.0f; > int i; > > for(i=0;i<10;i++) > f = f + 0.1f; > > if(f == 1.0f) > printf("f is 1.0 \n"); > else > printf("f is NOT 1.0\n"); > > return 0; > } -- Uriel Corfa -- To unsubscribe from this list: send the line "unsubscribe linux-c-programming" in the body of a message to majordomo@vger.kernel.org More majordomo info at http://vger.kernel.org/majordomo-info.html ^ permalink raw reply [flat|nested] 20+ messages in thread
* Re: float 2011-01-11 7:55 ` float Uriel Corfa @ 2011-01-11 8:01 ` Thomas Weber 2011-01-11 8:34 ` float ratheesh k 0 siblings, 1 reply; 20+ messages in thread From: Thomas Weber @ 2011-01-11 8:01 UTC (permalink / raw) To: Uriel Corfa; +Cc: ratheesh k, linux-c-programming Am 11.01.2011 08:55, schrieb Uriel Corfa: > I'd say : rounding errors. Floating point goes in mysterious ways. > > On Tue, Jan 11, 2011 at 8:38 AM, ratheesh k <ratheesh.ksz@gmail.com> wrote: >> I could not understand why it getting printed like this. Could any >> body tell me. >> >> #include <stdio.h> >> >> int main() >> { >> float f=0.0f; >> int i; >> >> for(i=0;i<10;i++) >> f = f + 0.1f; >> >> if(f == 1.0f) >> printf("f is 1.0 \n"); >> else >> printf("f is NOT 1.0\n"); >> >> return 0; >> } > Okay, now I got it. Look for IEEE754. 0.1 is in double precision 0.10000000149011612 Thomas ^ permalink raw reply [flat|nested] 20+ messages in thread
* Re: float 2011-01-11 8:01 ` float Thomas Weber @ 2011-01-11 8:34 ` ratheesh k 2011-01-11 8:57 ` float Thomas Weber 0 siblings, 1 reply; 20+ messages in thread From: ratheesh k @ 2011-01-11 8:34 UTC (permalink / raw) To: Thomas Weber; +Cc: Uriel Corfa, linux-c-programming On Tue, Jan 11, 2011 at 1:31 PM, Thomas Weber <thomas.weber.linux@googlemail.com> wrote: > Okay, now I got it. > > Look for IEEE754. > > 0.1 is in double precision 0.10000000149011612 > > Thomas > Is it possible to print the correct "printf" statement by typecasting some of operations or data types. I tried with no success. -Ratheesh > Am 11.01.2011 08:55, schrieb Uriel Corfa: >> I'd say : rounding errors. Floating point goes in mysterious ways. >> >> On Tue, Jan 11, 2011 at 8:38 AM, ratheesh k <ratheesh.ksz@gmail.com> wrote: >>> I could not understand why it getting printed like this. Could any >>> body tell me. >>> >>> #include <stdio.h> >>> >>> int main() >>> { >>> float f=0.0f; >>> int i; >>> >>> for(i=0;i<10;i++) >>> f = f + 0.1f; >>> >>> if(f == 1.0f) >>> printf("f is 1.0 \n"); >>> else >>> printf("f is NOT 1.0\n"); >>> >>> return 0; >>> } >> > -- To unsubscribe from this list: send the line "unsubscribe linux-c-programming" in the body of a message to majordomo@vger.kernel.org More majordomo info at http://vger.kernel.org/majordomo-info.html ^ permalink raw reply [flat|nested] 20+ messages in thread
* Re: float 2011-01-11 8:34 ` float ratheesh k @ 2011-01-11 8:57 ` Thomas Weber 0 siblings, 0 replies; 20+ messages in thread From: Thomas Weber @ 2011-01-11 8:57 UTC (permalink / raw) To: ratheesh k; +Cc: Thomas Weber, Uriel Corfa, linux-c-programming Am 11.01.2011 09:34, schrieb ratheesh k: > On Tue, Jan 11, 2011 at 1:31 PM, Thomas Weber > <thomas.weber.linux@googlemail.com> wrote: >> Okay, now I got it. >> >> Look for IEEE754. >> >> 0.1 is in double precision 0.10000000149011612 >> >> Thomas >> > > Is it possible to print the correct "printf" statement by typecasting > some of operations or data types. I tried with no success. > > -Ratheesh Try something with precision and typecast float to double printf("f:%.10f is 1.0 \n",(double) f); Thomas > > >> Am 11.01.2011 08:55, schrieb Uriel Corfa: >>> I'd say : rounding errors. Floating point goes in mysterious ways. >>> >>> On Tue, Jan 11, 2011 at 8:38 AM, ratheesh k <ratheesh.ksz@gmail.com> wrote: >>>> I could not understand why it getting printed like this. Could any >>>> body tell me. >>>> >>>> #include <stdio.h> >>>> >>>> int main() >>>> { >>>> float f=0.0f; >>>> int i; >>>> >>>> for(i=0;i<10;i++) >>>> f = f + 0.1f; >>>> >>>> if(f == 1.0f) >>>> printf("f is 1.0 \n"); >>>> else >>>> printf("f is NOT 1.0\n"); >>>> >>>> return 0; >>>> } >>> >> ^ permalink raw reply [flat|nested] 20+ messages in thread
* Re: float 2011-01-11 7:38 float ratheesh k 2011-01-11 7:46 ` float Thomas Weber 2011-01-11 7:55 ` float Uriel Corfa @ 2011-01-11 8:38 ` Andrej Gelenberg 2 siblings, 0 replies; 20+ messages in thread From: Andrej Gelenberg @ 2011-01-11 8:38 UTC (permalink / raw) To: ratheesh k; +Cc: linux-c-programming Hi, you can not check if 2 float are equal, you should check if it in the range: On 01/11/2011 08:38 AM, ratheesh k wrote: > I could not understand why it getting printed like this. Could any > body tell me. > > #include<stdio.h> > #define ABS(x) ((x) < 0 ? -(x) : (x)) #define MAX(a, b) ((a) > (b) ? (a) : (b)) const float precision = 0.001f; > int main() > { > float f=0.0f; > int i; > > for(i=0;i<10;i++) > f = f + 0.1f; > if ( ABS(f - 1.0f) < precision ) > printf("f is 1.0 \n"); > else > printf("f is NOT 1.0\n"); > > return 0; > } Here more to that topic: https://www.securecoding.cert.org/confluence/display/seccode/FLP35-C.+Take+granularity+into+account+when+comparing+floating+point+values Here more about c pitfalls: https://www.securecoding.cert.org/confluence/display/seccode/CERT+C+Secure+Coding+Standard Regards, Andrej Gelenberg ^ permalink raw reply [flat|nested] 20+ messages in thread
end of thread, other threads:[~2011-01-11 8:57 UTC | newest] Thread overview: 20+ messages (download: mbox.gz / follow: Atom feed) -- links below jump to the message on this page -- 2010-01-08 16:30 float phoenixxz 2010-01-08 4:42 ` float Bryan Christ 2010-01-08 16:45 ` float Fawad Lateef 2010-01-08 16:40 ` float Nanakos Chrysostomos 2010-01-08 16:44 ` float Fawad Lateef 2010-01-08 16:47 ` float Nanakos Chrysostomos 2010-01-08 17:11 ` float Michal Nazarewicz 2010-01-08 17:19 ` float Nanakos Chrysostomos 2010-01-08 17:21 ` float Nanakos Chrysostomos 2010-01-08 17:43 ` float Michal Nazarewicz 2010-01-08 18:00 ` float andrej.gelenberg 2010-01-08 22:05 ` float Glynn Clements 2011-01-11 7:38 float ratheesh k 2011-01-11 7:46 ` float Thomas Weber 2011-01-11 7:56 ` float Amit Virdi 2011-01-11 7:55 ` float Uriel Corfa 2011-01-11 8:01 ` float Thomas Weber 2011-01-11 8:34 ` float ratheesh k 2011-01-11 8:57 ` float Thomas Weber 2011-01-11 8:38 ` float Andrej Gelenberg
This is an external index of several public inboxes, see mirroring instructions on how to clone and mirror all data and code used by this external index.