# Floating point division

Started by April 6, 2013
Hi

how to divide two numbers.e,g if a = 4.2,b=2.35 how to divide separately the integer part and the fractional part and combine the result.

Regards,
Mayank
Mayank,

Perhaps if you explained why you are wanting to handle the integer and
fractional part separately, we could be of more help.

Are you actually trying to perform Fixed Point math?

The following code performs that you say you are asking for, but the result is
meaningless:
int integer1;
int fraction1;

int integer2;
int fraction2;

float result1;
float result2:

float integer3;
float integer4

float final;

integer1 = floor(4.2);
integer2 = floor(2.35);

fraction1 = 100*(4.2-integer1);
fraction2 = 100*(2.35-integer2);

result1 = integer1/integer2;
result2 = fraction1/fraction2;

final = result1+(result2/100);

R. Williams
---------- Original Message -----------
From: mayank agarwal
To: "c..."
Sent: Fri, 5 Apr 2013 21:09:22 -0700 (PDT)
Subject: [c6x] Floating point division

> Hi
>
> how to divide two numbers.e,g if a = 4.2,b=2.35 how to divide
> separately the integer part and the fractional part and combine the result.
>
> Regards,
> Mayank
------- End of Original Message -------

_____________________________________
mayank,
with float a; and float b:
you should be able to simply say.
float c = a/b;

then the compiler will perform the math using floating point.

Note;
if the DSP is fixed point, then you will need to include a floating point
library, both at compile time '#include ' and '#include '
and at link time '-lm'

Note:
The floating point libraries are very large.

R. Williams

---------- Original Message -----------
From: mayank agarwal
To: Richard Williams
Cc: "c..."
Sent: Sat, 6 Apr 2013 08:23:40 -0700 (PDT)
Subject: Re: [c6x] Floating point division

> Hi Richard,
>
> Thanks for your mail.The problem is like that gien below.
>
> Let's say, I give you a function called div_int which takes two
> integer arguments, x and y and gives a result which is the integer
> divisionof the two numbers. E.g. if x = 4 and y = 2 the answer is 2.
> If x = 5 and y = 2 the answer is 2. If x = 10 and y = 3 the answer is
> 3 etc.
>
> How will you use this div_int function to compute floating point divisions?
>
> Here is part of the C-code
>
> void main(void)
> {
>     float a = 0.1;
>     float b = 0.2;  // Do the same thing with a = 4.2 and b = 2.35
>
>     // fill in the blanks
>    _____;
>    .
>    .
>    .
>    _____;
>    __ = div_int(__, __);    // fill in the blanks
>
>    // fill in some more blanks
>    ______;
>    .
>    .
>    .
>    ______;
>
>    printf("The integer part of the divisionis %d and the fractional
> part of the divisionis %d\n", __, __);  // fill in the blanks }
>
> Thanks and Regards
> Mayank
>
> ________________________________
> From: Richard Williams
> To: mayank agarwal ; "c..."
> Sent: Saturday, April 6, 2013 8:40 PM Subject:
> Re: [c6x] Floating point division
>
> Mayank,
>
> Perhaps if you explained why you are wanting to handle the integer and
> fractional part separately, we could be of more help.
>
> Are you actually trying to perform Fixed Point math?
>
> The following code performs that you say you are asking for, but the
> result is meaningless:
>
> int integer1;
> int fraction1;
>
> int integer2;
> int fraction2;
>
> float result1;
> float result2:
>
> float integer3;
> float integer4
>
> float final;
>
> integer1 = floor(4.2);
> integer2 = floor(2.35);
>
> fraction1 = 100*(4.2-integer1);
> fraction2 = 100*(2.35-integer2);
>
> result1 = integer1/integer2;
> result2 = fraction1/fraction2;
>
> final = result1+(result2/100);
>
> R. Williams
>
> ---------- Original Message -----------
> From: mayank agarwal
> To: "c..."
> Sent: Fri, 5 Apr 2013 21:09:22 -0700 (PDT)
> Subject: [c6x] Floating point division
>
> > Hi
> >
> > how to divide two numbers.e,g if a = 4.2,b=2.35 how to divide
> > separately the integer part and the fractional part and combine the result.
> >
> > Regards,
> > Mayank
> ------- End of Original Message -------
------- End of Original Message -------

_____________________________________
> Subject: Floating point division
> Posted by: "mayank agarwal" m...@yahoo.com
> Date: Fri Apr 5, 2013 9:30 pm ((PDT))
>
> Hi
>
> how to divide two numbers.e,g if a = 4.2,b=2.35 how to divide separately the
> integer part and the fractional part and combine the result.
>
> Regards,
> Mayank
>

Hi Mayank,

You might want to scale up the numbers so that both are integers and
keep the exponent in mind to scale down the result.

Rgds,

Andrew

_____________________________________
Hi

Could you please illustrate floating point division using a/b where a = 4.2 and b=3.5
Regards
Mayank
________________________________
From: Andrew Nesterov
To: c...
Cc: mayank agarwal
Sent: Sunday, April 7, 2013 1:08 AM
Subject: [c6x] Re: Floating point division

> Subject: Floating point division
> Posted by: "mayank agarwal" m...@yahoo.com
> Date: Fri Apr 5, 2013 9:30 pm ((PDT))
>
> Hi
>
> how to divide two numbers.e,g if a = 4.2,b=2.35 how to divide separately the
> integer part and the fractional part and combine the result.
>
> Regards,
> Mayank
>

Hi Mayank,

You might want to scale up the numbers so that both are integers and
keep the exponent in mind to scale down the result.

Rgds,

Andrew
Hi Richard,

Thanks for your mail.The problem is like that gien below.

Let's say, I give you a function called div_int which takes two integer arguments, x and y and gives a result which is the integer divisionof the two numbers. E.g. if x = 4 and y = 2 the answer is 2. If x = 5 and y = 2 the answer is 2. If x = 10 and y = 3 the answer is 3 etc.

How will you use this div_int function to compute floating point divisions?

Here is part of the C-code

void main(void)
{
float a = 0.1;
float b = 0.2; // Do the same thing with a = 4.2 and b = 2.35

// fill in the blanks
_____;
.
.
.
_____;
__ = div_int(__, __); // fill in the blanks

// fill in some more blanks
______;
.
.
.
______;

printf("The integer part of the divisionis %d and the fractional part of the divisionis %d\n", __, __); // fill in the blanks
}

Thanks and Regards
Mayank
________________________________
From: Richard Williams
To: mayank agarwal ; "c..."
Sent: Saturday, April 6, 2013 8:40 PM
Subject: Re: [c6x] Floating point division

Mayank,

Perhaps if you explained why you are wanting to handle the integer and
fractional part separately, we could be of more help.

Are you actually trying to perform Fixed Point math?

The following code performs that you say you are asking for, but the result is
meaningless:
int integer1;
int fraction1;

int integer2;
int fraction2;

float result1;
float result2:

float integer3;
float integer4

float final;

integer1 = floor(4.2);
integer2 = floor(2.35);

fraction1 = 100*(4.2-integer1);
fraction2 = 100*(2.35-integer2);

result1 = integer1/integer2;
result2 = fraction1/fraction2;

final = result1+(result2/100);

R. Williams
---------- Original Message -----------
From: mayank agarwal
To: "c..."
Sent: Fri, 5 Apr 2013 21:09:22 -0700 (PDT)
Subject: [c6x] Floating point division

> Hi
>
> how to divide two numbers.e,g if a = 4.2,b=2.35 how to divide
> separately the integer part and the fractional part and combine the result.
>
> Regards,
> Mayank
------- End of Original Message -------
mayank,

If I understand you correctly, you want to perform an integer division (on
integers), and display both the result and the remainder.

For positive integers...
int a,b, result, remainder;
a=???
b=???
result = a / b;
remainder = a % b;

for negative integers (one or both)
int count = 0;

result = a / b;

if ( a < 0 )
{
count++;
a *= -1;
}
if ( b < 0 )
{
count++;
b *= -1;
}

remainder = a % b;
if ( count&0x01 ) remainder *= -1;

To perform the same kind of action on float values.
first decide how many decimal places to keep, lets use 3 decimal places

int a_int = floor(a*1000.0);
int b_int = floor(b*1000.0);

result = a_int / b_int;
remainder = a_int % b_int;
You could divide 'remainder' by 1000.0 to get the original leftover amount.
R. Williams
---------- Original Message -----------
From: mayank agarwal
To: Richard Williams
Cc: "c..."
Sent: Sat, 6 Apr 2013 08:23:40 -0700 (PDT)
Subject: Re: [c6x] Floating point division

> Hi Richard,
>
> Thanks for your mail.The problem is like that gien below.
>
> Let's say, I give you a function called div_int which takes two
> integer arguments, x and y and gives a result which is the integer
> divisionof the two numbers. E.g. if x = 4 and y = 2 the answer is 2.
> If x = 5 and y = 2 the answer is 2. If x = 10 and y = 3 the answer is
> 3 etc.
>
> How will you use this div_int function to compute floating point divisions?
>
> Here is part of the C-code
>
> void main(void)
> {
>     float a = 0.1;
>     float b = 0.2;  // Do the same thing with a = 4.2 and b = 2.35
>
>     // fill in the blanks
>    _____;
>    .
>    .
>    .
>    _____;
>    __ = div_int(__, __);    // fill in the blanks
>
>    // fill in some more blanks
>    ______;
>    .
>    .
>    .
>    ______;
>
>    printf("The integer part of the divisionis %d and the fractional
> part of the divisionis %d\n", __, __);  // fill in the blanks }
>
> Thanks and Regards
> Mayank
>
> ________________________________
> From: Richard Williams
> To: mayank agarwal ; "c..."
> Sent: Saturday, April 6, 2013 8:40 PM Subject:
> Re: [c6x] Floating point division
>
> Mayank,
>
> Perhaps if you explained why you are wanting to handle the integer and
> fractional part separately, we could be of more help.
>
> Are you actually trying to perform Fixed Point math?
>
> The following code performs that you say you are asking for, but the
> result is meaningless:
>
> int integer1;
> int fraction1;
>
> int integer2;
> int fraction2;
>
> float result1;
> float result2:
>
> float integer3;
> float integer4
>
> float final;
>
> integer1 = floor(4.2);
> integer2 = floor(2.35);
>
> fraction1 = 100*(4.2-integer1);
> fraction2 = 100*(2.35-integer2);
>
> result1 = integer1/integer2;
> result2 = fraction1/fraction2;
>
> final = result1+(result2/100);
>
> R. Williams
>
> ---------- Original Message -----------
> From: mayank agarwal
> To: "c..."
> Sent: Fri, 5 Apr 2013 21:09:22 -0700 (PDT)
> Subject: [c6x] Floating point division
>
> > Hi
> >
> > how to divide two numbers.e,g if a = 4.2,b=2.35 how to divide
> > separately the integer part and the fractional part and combine the result.
> >
> > Regards,
> > Mayank
> ------- End of Original Message -------
------- End of Original Message -------

_____________________________________
mayank,

your might want to google some examples of floating point division.
For instance,

when you go to the following, there are several side bar items regarding things
that need to be taken into account.

R. Williams
---------- Original Message -----------
From: mayank agarwal
To: Andrew Nesterov
Cc: "c..."
Sent: Sat, 6 Apr 2013 12:55:40 -0700 (PDT)
Subject: Re: [c6x] Re: Floating point division

> Hi
>
> Could you please illustrate floating point division using a/b where a
> = 4.2 and b=3.5 Regards Mayank
>
> ________________________________
> From: Andrew Nesterov
> To: c...
> Cc: mayank agarwal
> Sent: Sunday, April 7, 2013 1:08 AM
> Subject: [c6x] Re: Floating point division
>
>
>
> > Subject: Floating point division
> > Posted by: "mayank agarwal" m...@yahoo.com
> > Date: Fri Apr 5, 2013 9:30 pm ((PDT))
> >
> > Hi
> >
> > how to divide two numbers.e,g if a = 4.2,b=2.35 how to divide separately the
> > integer part and the fractional part and combine the result.
> >
> > Regards,
> > Mayank
> > Hi Mayank,
>
> You might want to scale up the numbers so that both are integers and
> keep the exponent in mind to scale down the result.
>
> Rgds,
>
> Andrew
------- End of Original Message -------

_____________________________________
> Date: 06-Apr-2013 12:55:40 -0700
> Cc: "c..."
> Subject: Re: [c6x] Re: Floating point division
>
> Hi
>
> Could you please illustrate floating point division using a/b where a = 4.2 and b=3.5

I mean that simple scaling: 4.2/3.5 = 42/35 (both scaled up by 10^1, result is
intact). Another example would be 4.2/0.35 = (42*10^(-1))/(35*10^(-2)) 42/35*10^1. It would be simplier to scale the numbers with powers of 2 being in
the binary format, such as IEEE-754.

Rgds,

Andrew

> Regards
> Mayank
> ________________________________
> From: Andrew Nesterov
> To: c...
> Cc: mayank agarwal
> Sent: Sunday, April 7, 2013 1:08 AM
> Subject: [c6x] Re: Floating point division
>> Subject: Floating point division
>> Posted by: "mayank agarwal" m...@yahoo.com
>> Date: Fri Apr 5, 2013 9:30 pm ((PDT))
>>
>> Hi
>>
>> how to divide two numbers.e,g if a = 4.2,b=2.35 how to divide separately the
>> integer part and the fractional part and combine the result.
>>
>> Regards,
>> Mayank
>> Hi Mayank,
>
> You might want to scale up the numbers so that both are integers and
> keep the exponent in mind to scale down the result.
>
> Rgds,
>
> Andrew

_____________________________________