[prev in list] [next in list] [prev in thread] [next in thread] 

List:       r-sig-finance
Subject:    Re: [R-SIG-Finance] Question regarding floor and round
From:       "Gandhi, Puneet - RSCH AMRS" <p.gandhi () baml ! com>
Date:       2010-08-26 11:44:06
Message-ID: FEE323851BB2C840819D7A025609CF49EEE89B () ex2k ! bankofamerica ! com
[Download RAW message or body]


 Great,
That works perfectly.

Thanks a lot Martin
Puneet

-----Original Message-----
From: Martin Becker [mailto:martin.becker@mx.uni-saarland.de] 
Sent: Thursday, August 26, 2010 3:38 AM
To: Pierre Lapointe
Cc: Gandhi, Puneet - RSCH AMRS; r-sig-finance@stat.math.ethz.ch
Subject: Re: [R-SIG-Finance] Question regarding floor and round


  AFAIR, Excel does not comply with IEEE rounding (but R does), which 
may lead to undesirable results if Base is not exactly representable; at

least on my system I get

 > my.floor(2.8,0.2)
[1] 2.6

for example.
So (if my memory about Excel's behaviour is correct) one has to modify 
the functions my.floor and my.ceiling slightly to mimic Excel's 
behaviour more precisely. Something like

Floor <- function(x,Base) {
   Base*floor(x*(1+.Machine$double.eps)/Base)
}

Ceiling <- function(x,Base) {
   Base*ceiling(x*(1-.Machine$double.eps)/Base)
}

may be closer to Excel. At least, in the previous example I get

 > Floor(2.8,0.2)
[1] 2.8

on my system.

HTH

   Martin




On 26.08.2010 00:00, Pierre Lapointe wrote:
> Here are modified floor and ceiling functions that solve your problem:
>
> my.floor<-function(x,my.base){
> floor(x/my.base)*my.base
> }
>
> my.ceiling<-function(x,my.base){
> ceiling(x/my.base)*my.base
> }
>
> Base = 0.5
> my.floor(126.6,Base)   #[1] 126.5
> my.ceiling(126.6,Base) #[1] 127
>
> Base = 1
> my.floor(126.6,Base) #[1] 126
> my.ceiling(126.6,Base) #[1] 127
>
> HTH
>
> Pierre
>
>
> On Wed, Aug 25, 2010 at 4:38 PM, Gandhi, Puneet - RSCH AMRS<
> p.gandhi@baml.com>  wrote:
>
>> Hi All,
>>
>> Is there any function in R which does what Floor or Ceiling in Excel.
>>
>> Excel can take 2 arguments to FLOOR function while R doesn't.
>>
>> I need the following which is done in excel
>>
>> Base = 0.5
>> Floor(126.6,Base) = 126.5
>> Ceiling(126.6, Base) = 127
>>
>>
>> Base = 1
>> Floor(126.6, Base) = 126
>> Ceiling(126.6, Base) = 127
>>
>> I tried round, floor, ceiling, trunc in R but none does this job.
>>
>> Pls Help/Thanks
>> Puneet
>>
>>
----------------------------------------------------------------------
>> This message w/attachments (message) is intended solely
...{{dropped:7}}
>>
>> _______________________________________________
>> R-SIG-Finance@stat.math.ethz.ch mailing list
>> https://stat.ethz.ch/mailman/listinfo/r-sig-finance
>> -- Subscriber-posting only. If you want to post, subscribe first.
>> -- Also note that this is not the r-help list where general R
questions
>> should go.
>>
> 	[[alternative HTML version deleted]]
>
> _______________________________________________
> R-SIG-Finance@stat.math.ethz.ch mailing list
> https://stat.ethz.ch/mailman/listinfo/r-sig-finance
> -- Subscriber-posting only. If you want to post, subscribe first.
> -- Also note that this is not the r-help list where general R
questions should go.


-- 
Dr. Martin Becker
Statistics and Econometrics
Saarland University
Campus C3 1, Room 217
66123 Saarbruecken
Germany

----------------------------------------------------------------------
This message w/attachments (message) is intended solely ...{{dropped:7}}

_______________________________________________
R-SIG-Finance@stat.math.ethz.ch mailing list
https://stat.ethz.ch/mailman/listinfo/r-sig-finance
-- Subscriber-posting only. If you want to post, subscribe first.
-- Also note that this is not the r-help list where general R questions should go.
[prev in list] [next in list] [prev in thread] [next in thread] 

Configure | About | News | Add a list | Sponsored by KoreLogic