FAQ Database Discussion Community


Extracting negative and positive fractions from a string in C

c,fractions,negative-number
I have a line of string = VOLTAGE 0.231023459 CURRENT -0.234334567 0.345511234 How can I extract the negative fractions from the above line. For positive values I can #define the index of the string and start parsing it.But there is no fixed sign for the values, the values can be...

Converting decimal to fraction

java,split,double,decimal,fractions
I have a method that is converting a decimal (double value) into a fraction and putting the numerator and denominator values into an int[] of size 2. Testing it works out fine for most values except when I hit 0.0001. Then the return value is 1.0/1.0. The method: private static...

Java Fraction Addition Formula Trouble

java,math,fractions
I have a project for a class where you have to recieve two fractions from a user with different denominators, add them together and output them with the lowest common denominator. Since we have to use principles from only chapters we have covered I cannot make a fraction class so...

Javascript problems with a function not working (it converts fractions into decimals)

javascript,function,decimal,fractions
Let's say I have a string (like a fraction); var num = "1/2"; Why does this work: var y = num.split(' '); if (y.length > 1) { var z = y[1].split('/'); var a = (+y[0] + (z[0] / z[1])); num = a; } else { z = y[0].split('/'); if (z.length...

Is there a way to check if a private variable is null? C++

c++,math,fractions
I have been searching all over and could not find the answer to this. Basically imagine having a class like so in the header. class fractions { private: int numerator; int denominator; public: //etc etc }; Now I am in the main and want to access the denominator and numerator...

How to print void function after cout statement?

c++,function,fractions
I'm trying to print the output of the function after my values are sent to the function. The cout statement needs a string but I'm not sure how I can return a string from my reduce_fraction function while keeping the math correct. In my add_fraction function, you'll see that I...

How do you create a fraction in java [duplicate]

java,syntax,fractions
This question already has an answer here: Best way to represent a fraction in Java? 24 answers What would the syntax be to make a fraction, for example if I wanted to make a program that takes a fraction and adds it to another fraction. I am talking about...

Display operations on multiple fractions in a single line

java,fractions
The following code is working as expected, however I'm unable to display the output on a single line. All numbers are random numbers printed using Random() function. As of now, it's printing like this: 38 --- + 45 90 --- + 16 29 --- + 21 54 --- + 99...

fraction operator overloading

c++,operator-overloading,fractions
I am new in C++ and I have a problem with operator overloading. I just implemented the functions in header file.I want to add two fractional numbers simplest form to each other.I implemented a gcd for simplification and implemented operator + for addinf the together bu I got an error...

Java fraction calculator throws exception for division by zero when trying to simplify

exception,fractions,divide-by-zero
I have the following class which I am trying to use to perform calculations between fractions however every now and again i get a division by zero exception from on the simplify function and i cannot work out why it is doing it public class Fraction { private int top;...