1
$\begingroup$

I made something in excel that calculates the days left until a given date, and from that how many weeks were left. I had it so that 9 days displayed as 1.2 using this formula: $$\frac{\left(\frac{A}{B}-\Bigl\lfloor{\frac{A}{B}}\Bigr\rfloor\right)\cdot B}{10}+\Bigl\lfloor{\frac{A}{B}}\Bigr\rfloor$$

Where $B$ is the "base" you are counting in and $A$ is the number you are trying to count.

This all works and the question is purely for personal interest only.

So, Is there a way to calculate the Integer portion of a fraction only using the operators +, -, $\div$ and *? Thanks In Advance

$\endgroup$
6
  • 1
    $\begingroup$ Please use LaTex!!! $\endgroup$ Commented Mar 23, 2015 at 11:45
  • $\begingroup$ Two other functions come to mind that have roughly the same effect: CEILING and TRUNCATE. All three are doing an "integer modulus" operation, which in another programming environment might be accomplished by casting to an int. Mathematically, these three functions are the main way to get at this information, as there isn't much else that distinguishes one side of the decimal point from the other. $\endgroup$
    – abiessu
    Commented Mar 23, 2015 at 11:50
  • $\begingroup$ Sorry @BarakManos was trying to figure out how to use it, so I thought I'd post the question and then change it :) $\endgroup$
    – Rinslep
    Commented Mar 23, 2015 at 12:06
  • $\begingroup$ @abiessu , I know the functions that work with decimals and integers, I'm more wondering how to do it without using these functions. $\endgroup$
    – Rinslep
    Commented Mar 23, 2015 at 12:06
  • $\begingroup$ The answer to the question in the title is $\Bigl\lfloor{\frac{A}{B}}\Bigr\rfloor=\frac{A-A\bmod{B}}{B}$. The question in the body I do not understand, perhaps you can think of a way to clarify it. $\endgroup$ Commented Mar 23, 2015 at 12:18

1 Answer 1

0
$\begingroup$

You can take advantage of the fact that modulo is the same as the quotient operator for positive values and then sign correct the result. Not the most elegant solution but something like this should work.

$$ \left \lfloor{\frac{A}{B}}\right \rfloor = \left\{\begin{array}{lr} \frac{|A| - |A| \% |B|}{|B|} , & \text{for sgn(A) } = \text{sgn(B)} \\ -\frac{|A| - |A| \% |B|}{|B|} - 1, & \text{for sgn(A) } \neq \text{sgn(B)} \end{array} \right\} $$

where sgn is the signum function.

$\endgroup$

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .