ALT Linux Community general discussions
 help / color / mirror / Atom feed
* [Comm] c language question
@ 2006-11-04 16:14 Andrey Rybak
  2006-11-04 16:18 ` Eugene Ostapets
  0 siblings, 1 reply; 13+ messages in thread
From: Andrey Rybak @ 2006-11-04 16:14 UTC (permalink / raw)
  To: community

Привет, коммунити!
Извините за оффтоп.
Стоит задача (стандартная должно быть) преобразовывать строки  вида
"0.ab" в целое число ab.
Вероятно, я плохо понимаю Си , но самый прямой метод будет выглядеть так:
      1 #include<stdio.h>
      2 #include<stdlib.h>
      3 int main(){
      4         const char* a = "0.16";
      5         printf("%d",(int)(100*atof(a)));
      6         return 0;
      7 }
Но вся беда в том, что это не всегда работает. Например atof запросто
может вернуть 0.159999... в приведенном примере, а приведение типа (int)
просто отбрасывает дробную часть и результат налицо: "0.16"
преобразуется в целое 15.
Проблема наверняка стара как сам Си. Кто знает, как классики решают
подобные задачи?


^ permalink raw reply	[flat|nested] 13+ messages in thread

end of thread, other threads:[~2006-11-04 20:32 UTC | newest]

Thread overview: 13+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2006-11-04 16:14 [Comm] c language question Andrey Rybak
2006-11-04 16:18 ` Eugene Ostapets
2006-11-04 17:39   ` Хихин Руслан
2006-11-04 17:59     ` Eugene Ostapets
2006-11-04 18:41       ` Хихин Руслан
2006-11-04 18:55         ` Michael Holzman
2006-11-04 19:17           ` Хихин Руслан
2006-11-04 20:32             ` Michael Holzman
2006-11-04 17:44   ` Andrey Rybak
2006-11-04 17:53     ` Eugene Ostapets
2006-11-04 18:24       ` Andrey Rybak
2006-11-04 18:30         ` Eugene Ostapets
2006-11-04 18:51           ` Andrey Rybak

ALT Linux Community general discussions

This inbox may be cloned and mirrored by anyone:

	git clone --mirror http://lore.altlinux.org/community/0 community/git/0.git

	# If you have public-inbox 1.1+ installed, you may
	# initialize and index your mirror using the following commands:
	public-inbox-init -V2 community community/ http://lore.altlinux.org/community \
		mandrake-russian@linuxteam.iplabs.ru community@lists.altlinux.org community@lists.altlinux.ru community@lists.altlinux.com
	public-inbox-index community

Example config snippet for mirrors.
Newsgroup available over NNTP:
	nntp://lore.altlinux.org/org.altlinux.lists.community


AGPL code for this site: git clone https://public-inbox.org/public-inbox.git