delorie.com/archives/browse.cgi   search  
Mail Archives: djgpp/1998/01/20/08:25:32

From: "A. Sinan Unur" <sinan DOT unur AT cornell DOT edu>
Newsgroups: comp.os.msdos.djgpp
Subject: Re: round
Date: Mon, 19 Jan 1998 17:11:02 -0500
Organization: Cornell University (http://www.cornell.edu/)
Lines: 35
Sender: asu1 AT cornell DOT edu (Verified)
Message-ID: <34C3CF76.F0A9A2DE@cornell.edu>
References: <34c3be38 DOT 741112 AT nntpserver DOT swip DOT net>
NNTP-Posting-Host: cu-dialup-0041.cit.cornell.edu
Mime-Version: 1.0
To: djgpp AT delorie DOT com
DJ-Gateway: from newsgroup comp.os.msdos.djgpp

Rikard Björklind wrote:
> 
> I want to know how to assign a double value to an int. Like this for
> example:
> 
> int hej = (PI*sin(34)/43);
> 
> I want the value rounded. Please help me I'm a beginner =).

hej,

first of all, this is a basic C question and hence off-topic. you should
give news:comp.lang.c and/or news:comp.lang.c.moderated a try in the
future, and while you are at it, download the C faq also. in any case,
now that you are here, note the following:

* in ANSI C, arguments to the trigonometric functions are given in
radians, not degrees (360 degrees = 2*PI radians). the header math.h
defines the constants M_PI (180 dg), M_PI_2 (90 dg) and M_PI_4 (45 dg),
use those.

* the best way to proceed depends on what you understand by rounding. if
d is a double value, (int)(d+0.5) is usually reasonable. you might also
want to look at the documentation for floor and ceil functions to see if
that is the functionality you are looking for.

hth.
-- 
----------------------------------------------------------------------
A. Sinan Unur
Department of Policy Analysis and Management, College of Human Ecology,
Cornell University, Ithaca, NY 14853, USA

mailto:sinan DOT unur AT cornell DOT edu
http://www.people.cornell.edu/pages/asu1/

- Raw text -


  webmaster     delorie software   privacy  
  Copyright © 2019   by DJ Delorie     Updated Jul 2019