Path: news.mathworks.com!not-for-mail
From: <HIDDEN>
Newsgroups: comp.soft-sys.matlab
Subject: Re: Help! Rounding without using preset MATLAB functions
Date: Sat, 20 Nov 2010 23:34:04 +0000 (UTC)
Organization: The MathWorks, Inc.
Lines: 22
Message-ID: <ic9ltc$rtb$1@fred.mathworks.com>
References: <ic9io4$792$1@fred.mathworks.com>
Reply-To: <HIDDEN>
NNTP-Posting-Host: webapp-05-blr.mathworks.com
Content-Type: text/plain; charset=UTF-8; format=flowed
Content-Transfer-Encoding: 8bit
X-Trace: fred.mathworks.com 1290296044 28587 172.30.248.35 (20 Nov 2010 23:34:04 GMT)
X-Complaints-To: news@mathworks.com
NNTP-Posting-Date: Sat, 20 Nov 2010 23:34:04 +0000 (UTC)
X-Newsreader: MATLAB Central Newsreader 1187260
Xref: news.mathworks.com comp.soft-sys.matlab:688648

"Steven " <stevenlwillis@gmail.com> wrote in message <ic9io4$792$1@fred.mathworks.com>...
> Hi everybody, 
> 
> I'm supposed to write a code that will round any values to a specified numeric placement, such as the tens, hundredths, etc...  without using any built in functions such as 'round','ceil',etc.. I'm at a complete loss. 
> 
> Say, for example I'm testing on a single value 10.23, and I wanted to round it to 10.2. 
> 
>  I have no idea how to make MATLAB check to see if it should round up or down, or how to add or subtract the correct numbers to get to where I need it. It's one of those times where I'm pretty sure if I get one good brainwave I'll be able to get a lot more done, but for now, I'm just sitting here. 
> 
> I suppose I'm essentially trying to write the 'round' 'ceil' and 'floor' functions from the ground up. 
> 
> I really appreciate any advice!
> 
> Steve
- - - - - - - - - - -
  You could approximate the 'round' function with

 y = x - atan(tan(pi*x))/pi;

However, it does seem rather absurd allowing 'tan' and 'atan' if one is not allowed to use 'round'.  Also it suffers a serious accuracy loss when x is near the halfway point between integers.

Roger Stafford