473,721 Members | 2,295 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Simplyfiying fractions in Javascript

Does anybody have a script to simplify fractions?

Jan 13 '06 #1
8 4550
Or just like a recommendation?

Jan 13 '06 #2

mo********@gmai l.com wrote:
Or just like a recommendation?

What exactly do you need? Please provide sample input/output...

Jan 13 '06 #3
mo********@gmai l.com wrote:
Does anybody have a script to simplify fractions?


To simplify a fraction, find the greatest common divisor of the numerator
and the denominator of the fraction and divide both by it if there is one
greater than 1.

<URL:http://google.com/search?q=simpli fy+fraction&fil ter=0>
HTH

PointedEars
Jan 13 '06 #4
JRS: In article <11************ **********@g14g 2000cwa.googleg roups.com>
, dated Thu, 12 Jan 2006 17:52:27 local, seen in
news:comp.lang. javascript, mo********@gmai l.com <mo********@gma il.com>
posted :
Does anybody have a script to simplify fractions?


Let the fraction be X/Y, X & Y being variables.

function HCF(u, v) { var U = u, V = v
while (true) {
if (!(U%=V)) return V
if (!(V%=U)) return U } }

T = HCF(X, Y) ; X /= T ; Y /= T ;

HCF is used in js-dates.htm, via FAQ, see below. It is intended for use
only with positive integer parameters, but full compliance is not
obligatory.

--
© John Stockton, Surrey, UK. ?@merlyn.demon. co.uk Turnpike v4.00 IE 4 ©
<URL:http://www.jibbering.c om/faq/> JL/RC: FAQ of news:comp.lang. javascript
<URL:http://www.merlyn.demo n.co.uk/js-index.htm> jscr maths, dates, sources.
<URL:http://www.merlyn.demo n.co.uk/> TP/BP/Delphi/jscr/&c, FAQ items, links.
Jan 13 '06 #5
Dr John Stockton said the following on 1/13/2006 3:03 PM:
JRS: In article <11************ **********@g14g 2000cwa.googleg roups.com>
, dated Thu, 12 Jan 2006 17:52:27 local, seen in
news:comp.lang. javascript, mo********@gmai l.com <mo********@gma il.com>
posted :
Does anybody have a script to simplify fractions?

Let the fraction be X/Y, X & Y being variables.

function HCF(u, v) { var U = u, V = v


Just for my own curiosity, does the U = u and V = v serve any useful
purpose? Meaning, does the code fail if you use u and v below instead of
U and V?

It shouldn't is why I am asking.
while (true) {
if (!(U%=V)) return V
if (!(V%=U)) return U } }

T = HCF(X, Y) ; X /= T ; Y /= T ;

HCF is used in js-dates.htm, via FAQ, see below. It is intended for use
only with positive integer parameters, but full compliance is not
obligatory.

--
Randy
comp.lang.javas cript FAQ - http://jibbering.com/faq & newsgroup weekly
Javascript Best Practices - http://www.JavascriptToolbox.com/bestpractices/
Jan 14 '06 #6
Randy Webb <Hi************ @aol.com> writes:
Dr John Stockton said the following on 1/13/2006 3:03 PM:

function HCF(u, v) { var U = u, V = v


Just for my own curiosity, does the U = u and V = v serve any useful
purpose? Meaning, does the code fail if you use u and v below instead
of U and V?


Not in this case. Both "u" and "U" are local variables, and since "u"
is never used again, there is no immediate need to rename it.

I'm guessing it's a general strategy to avoid changing the parameters,
in case they are needed again later.

/L
--
Lasse Reichstein Nielsen - lr*@hotpop.com
DHTML Death Colors: <URL:http://www.infimum.dk/HTML/rasterTriangleD OM.html>
'Faith without judgement merely degrades the spirit divine.'
Jan 14 '06 #7
thanks all

Jan 14 '06 #8
JRS: In article <W6************ ********@comcas t.com>, dated Fri, 13 Jan
2006 21:38:20 local, seen in news:comp.lang. javascript, Randy Webb
<Hi************ @aol.com> posted :
Dr John Stockton said the following on 1/13/2006 3:03 PM:

Let the fraction be X/Y, X & Y being variables.

function HCF(u, v) { var U = u, V = v


Just for my own curiosity, does the U = u and V = v serve any useful
purpose? Meaning, does the code fail if you use u and v below instead of
U and V?

It shouldn't is why I am asking.
while (true) {
if (!(U%=V)) return V
if (!(V%=U)) return U } }

T = HCF(X, Y) ; X /= T ; Y /= T ;

HCF is used in js-dates.htm, via FAQ, see below. It is intended for use
only with positive integer parameters, but full compliance is not
obligatory.


Usually at least, ISTM that it will not fail. I converted it from
another language, in which there are three possible ways of passing
parameters and the one which copies must be used (another won't compile
with that body; the third will change the external quantities). I put
the copying in as being easier than deciding whether it might ever
matter.

The only javascript in which I at present use it is

F = A[0] ; J = N ; while (J--) F = HCF(F, A[J])

after which A is abandoned and F is displayed - so I'd not notice the
difference anyway even if there were any.

Code, in js-dates.htm, changed. Thanks.

<URL:http://www.merlyn.demo n.co.uk/hcfactor.pas> has several HCF
algorithms.

--
© John Stockton, Surrey, UK. ?@merlyn.demon. co.uk Turnpike v4.00 MIME. ©
Web <URL:http://www.merlyn.demo n.co.uk/> - FAQqish topics, acronyms & links.
PAS EXE TXT ZIP via <URL:http://www.merlyn.demo n.co.uk/programs/00index.htm>.
Do not Mail News to me. Before a reply, quote with ">" or "> " (SoRFC1036)
Jan 15 '06 #9

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

33
6624
by: selowitch | last post by:
I've been searching in vain for a way to present typographically correct fractions (not resorting to <sup> and <sub> tags) but have been frustrated by the fact that the glyphs for one-half, one-eighth, three-quarters, etc. do not display in the correct typeface (or even consistently the same typeface) and seem totally resistant to attempt to fix this through CSS: <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"...
5
2413
by: Steffen | last post by:
Hi, is it possible to have two fractions, which (mathematically) have the order a/b < c/d (a,b,c,d integers), but when (correctly) converted into floating point representation just have the opposite order? The idea is that the two fractions are almost identical and that the error introduced by going to floating point representation is bigger than the exact difference, but different for the two fractions such that it somehow turns...
2
7048
by: Mori | last post by:
Hi, Can someone supply a code example of displaying a string with a fractional part, say 5 and 7 16ths. I cannot find an example of how to use the Encoding object (if that is what you use). thanks, Mori
8
2040
by: Cogito | last post by:
I'm trying to understand some Javascript code from a certain site. Could you please tell me what does this test mean? if ( j%4 == 0 ) What does %4 test for and is there a web site that explains it and, presumably, also %1, %2 etc.
4
3377
by: Bob | last post by:
Hi All, Was wondering if in C# there is an elegant way of displaying and or calculating fractions. The case: we have an app that measures/slices dices etc and all our internal measures and counters are in metric measures ... (its a manufacturing tool) For display purposes this is fine for Australia/Europe/Asia .. however the US company likes to see the measures in feet/inches .. no real problem here.
1
1633
by: Semajthewise | last post by:
Here it is cleaned up a little more. Here's what this code does. It will take 2 fractions and add, subtract, multiply, or divide them. The user enters the fractions to be calculated into two textboxes. These can be entered as a whole number and a proper fraction, whole number and an improper fraction, just a proper/inproper fraction, or just a whole number. Form build Requirements: using Microsoft Visual Studio.NET 2003 single form...
5
5622
by: gubbachchi | last post by:
Hi all, How to convert the fractions into decimals and vice versa in php. I have a form, where the user will enter fractions in the text boxes such as "1 1/2", "1 1/4" and so on. I need to store all these values in mysql database after converting it into decimal value, and again fetch the value from database and re-convert it into fractions and display it to the user in a different php page. So what is best solution for this. Can anyone...
0
1242
by: Paddy | last post by:
(From: http://paddy3118.blogspot.com/2008/09/python-fractions-issue.html) There seems to be a problem/difference in calculating with the new fractions module when comparing Python 26rc2 and 30rc1 I was reading the paper "Interval Arithmetic: Python Implementation and Applications" and thought to try the first example function f(x,y), which needs more precision than Python floating point provides (on my Windows PC), to calculate a...
0
8851
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8736
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
9373
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
9077
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
8019
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6676
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5992
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4761
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
3
2137
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.