help-octave
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: (newbie) fractional arithmetic


From: Kevin Straight
Subject: Re: (newbie) fractional arithmetic
Date: Fri, 9 Feb 2001 16:39:44 -0800 (PST)

Don't really know what you're looking for, but in Goctave I have a
(trivial) function that converts a floating point number to a fraction:

One would think that, if you were willing to be a little cute, you could
represent your fractions as vectors, which would let multiplication and
addition work properly.  (although a lot of other things might not work so
well).

If that's not what you're thinking, I do know that there is a set of
symbolic functions floating around, that could probably be used to deal
with fractions.

---------

## usage: fraction(x)
##
## Attempt to convert a float to a fraction

## Author: Kevin Straight address@hidden

function retval = fraction (x)

     if(nargin != 1)
        usage("fraction (x)");
     endif

     denominator = 1;

     while(x!=floor(x))
        x=x*10;
        denominator=denominator*10;
     endwhile

     l=gcd(x, denominator);     
     while(l!=1)
        x=x/l;
        denominator=denominator/l;
        l=gcd(x, denominator);  
     endwhile

     retval = zeros(2,1);
     retval(1,1) = x;
     retval(2,1) = denominator;

endfunction

--------

On Fri, 9 Feb 2001, Joshua McFadden wrote:

> Hello!  Short version: can Octave perform fractional arithmetic?  E.g.,
>      1
>  x = -
>      9
> instead of 0.1111... as a floating double?
> 
> Quoting the manual, "Note that all numeric constants are represented
> within Octave in double-precision floating point form."  I was hoping
> someone had written a way around that.  :)
> 
> I'm trying to explore iteration periods of the doubling function for a
> class, and repeating decimals get lost in precision errors pretty quickly.
> If anyone can suggest a more appropriate solution for Linux besides
> shelling out for Mathematica, I'm all ears.
> 
> (Sorry if this is inane; I don't have the bandwith to search the
> help-octave archive manually.)
> 
> Thanks!
> 
> -Josh
> 
> 
> 
> -------------------------------------------------------------
> Octave is freely available under the terms of the GNU GPL.
> 
> Octave's home on the web:  http://www.octave.org
> How to fund new projects:  http://www.octave.org/funding.html
> Subscription information:  http://www.octave.org/archive.html
> -------------------------------------------------------------
> 

==========================
Kevin Straight
University of Idaho
www.uidaho.edu/~stra9456
==========================



-------------------------------------------------------------
Octave is freely available under the terms of the GNU GPL.

Octave's home on the web:  http://www.octave.org
How to fund new projects:  http://www.octave.org/funding.html
Subscription information:  http://www.octave.org/archive.html
-------------------------------------------------------------



reply via email to

[Prev in Thread] Current Thread [Next in Thread]