0

I'm trying to write an equation of a complexfraction32 which is 32bit.

I would like to test for 20% of the value for less than 20% and more than 20%

I have a function mult_fr1x32x32 which multiplies the complex fraction by a number. but complex fraction is from -1,1. How would I write the equation correctly.

if( (GO_coil_H[0].re*0.8f) < (CoilEepromData.coilboardhspule.reFundamental) && (CoilEepromData.coilboardhspule.reFundamental) <= (GO_coil_H[0].re)*1.2f)
{
    // do something
}

extern volatile complex_fract32     GO_coil_H[DSP_NUM_FREQ];

unsigned int CoilEepromData.coilboardhspule.reFundamental
implmentor
  • 1,386
  • 4
  • 21
  • 32
Andre
  • 51
  • 9
  • This is some chunk of code totally ripped out of it's context. We have no way to know what is this about. – Eugene Sh. Sep 26 '16 at 15:16
  • @EugeneSh. I have updated the code, and put the decelerations of the variables – Andre Sep 26 '16 at 15:23
  • @EugeneSh. I'm trying to check if one complex fraction variable is less than 20% or more than 20% of the unsigned int value. The problem is multiplying with complex fraction by 1.2 will exceed the limit of complex fracation data. I don't know how to write the equation correctly – Andre Sep 26 '16 at 15:24
  • To test if a value is in range you cannot use `(min < value <= max)`. You should use `((min < value) && (value <= max))`. How to handle the complex_fract32 type is another issue. – kkrambo Sep 26 '16 at 15:25
  • @kkrambo I have fixed it. thanks. but how to handle complex fract 32 – Andre Sep 26 '16 at 15:35
  • For the second part of the test, couldn't you use `(CoilEepromData.coilboardhspule.reFundamental/1.2) <= (GO_coil_H[0].re)`? – Ian Abbott Sep 26 '16 at 15:55
  • @IanAbbott is it ok to compare unsigned int, with complex fraction ? – Andre Sep 26 '16 at 15:57
  • @IanAbbott 1/1.2 = 0.83, I need to compare 20% of the value not 0.83 of the value – Andre Sep 26 '16 at 16:02
  • @Andre, well the comparison will be done after converting both sides of the comparison to `double`. You might want to replace `1.2` with `1.2f` to do the comparison as `float`. I'm guessing your `unsigned int CoilEepromData.coilboardhspule.reFundamental` is a fixed point fraction with the same number of fractional bits as `fract32`. – Ian Abbott Sep 26 '16 at 16:07
  • @Andre `x/1.2 <= y` is mathematically equivalent to `x <= y*1.2` - it's just moving the constant to the other side. In the real world of floating point arithmetic, there will probably be a subtle difference due to rounding errors. – Ian Abbott Sep 26 '16 at 16:10
  • @IanAbbott how about the 0.8 problem ? how to multiply it by 0.8 – Andre Sep 26 '16 at 16:15
  • @IanAbbott unsigned int CoilEepromData.coilboardhspule.reFundamental is not a fract32, its unsigned int, would be a problem ? or should I convert it to fract32 ? – Andre Sep 26 '16 at 16:19
  • @Andre, you can leave the second comparison as `(CoilEepromData.coilboardhspule.reFundamental) <= (GO_coil_H[0].re*1.2f)` if you want, as the comparison is done in proper floating point anyway. You don't need to convert `CoilEepromData.coilboardhspule.reFundamental` to `fract32` if it's value already represents a fraction with the same number of fraction bits as `fract32`. `fract32` seems to be defined by `typedef long fract32`. On Blackfin, `int` and `long` both seem to be 32-bit signed integer types. – Ian Abbott Sep 26 '16 at 16:27
  • So should i define the variable as int not as unsigned int. And how about the first part of the comparison. Thanks alt – Andre Sep 26 '16 at 16:48
  • @kkrambo can you help me please ? I'm confused about the complex fraction and how to use it to do the comparison correctly ? – Andre Sep 26 '16 at 18:24
  • @IanAbbott there is a float_to_fract, should I use it to conver 0.8 to fract and then multiply it using mult_fr1x32x32, and then do the comparison ? – Andre Sep 26 '16 at 18:49
  • @IanAbbott but float_to_fract32 of 1.2 will overflow the data – Andre Sep 26 '16 at 19:00
  • @Andre Perhaps it will be easier for you if you just convert the `fract32` values to `float` explicitly with the `fr32_to_float` function like this: `if( (fr32_to_float(GO_coil_H[0].re)*0.8f) < fr32_to_float(CoilEepromData.coilboardhspule.reFundamental) && fr32_to_float(CoilEepromData.coilboardhspule.reFundamental) <= fr32_to_float(GO_coil_H[0].re)*1.2f)` – Ian Abbott Sep 27 '16 at 14:13

0 Answers0