Shop OBEX P1 Docs P2 Docs Learn Events
Problems negating a number — Parallax Forums

Problems negating a number

reppigreppig Posts: 35
edited 2011-10-18 13:33 in Propeller 1
When I run the following code cos_lon evaluates correctly to 0.1618761.
However, neg_cos_lon evaluates to -27.27986 not -0.1618761 which is what I want??
I also tried what was in the manual -neg_cos_lon and I got the same wrong answer.
      cos_lon := f.Cos(f.Radians(gs_lon))
      neg_cos_lon := -cos_lon

     DBG.Str(FS.FloatToString(cos_lon))
     DBG.Str(STRING("   "))   
     DBG.Str(FS.FloatToString(neg_cos_lon))

Comments

  • Phil Pilgrim (PhiPi)Phil Pilgrim (PhiPi) Posts: 23,514
    edited 2011-10-18 12:10
    You're negating cos_lon, a floating-point number, in the integer domain. That does not work. You need to negate it (or subtract it from 0.0) using a floating-point method.

    -Phil
  • Bobb FwedBobb Fwed Posts: 1,119
    edited 2011-10-18 12:44
    Or just flip bit 31?

    Like:
    neg_cos_lon := cos_lon ^ $8000_0000

    Much faster than a floating point operation, and I can't think of a scenario where it would give you incorrect results (though I'm no expert on floating point).
  • reppigreppig Posts: 35
    edited 2011-10-18 13:09
    I understand what you are saying and it makes sense. The manual implied it just changed the sign of a number. So, should I assume everything the manual says only apply to integers??
  • reppigreppig Posts: 35
    edited 2011-10-18 13:10
    I will give it a try tommorrow. Thanks.
    Bobb Fwed wrote: »
    Or just flip bit 31?

    Like:
    neg_cos_lon := cos_lon ^ $8000_0000

    Much faster than a floating point operation, and I can't think of a scenario where it would give you incorrect results (though I'm no expert on floating point).
  • reppigreppig Posts: 35
    edited 2011-10-18 13:11
    I understand what you are saying and it makes sense. The manual implied it just changed the sign of a number. So, should I assume everything the manual says only apply to integers??
    You're negating cos_lon, a floating-point number, in the integer domain. That does not work. You need to negate it (or subtract it from 0.0) using a floating-point method.

    -Phil
  • Phil Pilgrim (PhiPi)Phil Pilgrim (PhiPi) Posts: 23,514
    edited 2011-10-18 13:14
    reppig wrote:
    The manual implied it just changed the sign of a number. So, should I assume everything the manual says only apply to integers??
    As it pertains to native Spin operations, yes.

    -Phil
  • Bobb FwedBobb Fwed Posts: 1,119
    edited 2011-10-18 13:33
    reppig wrote: »
    The manual implied it just changed the sign of a number. So, should I assume everything the manual says only apply to integers??
    As already said, yes, everything is assumed as integers.

    The problem with negating a float, when it thinks it is a integer is because the bit structure for an integer of negative and positive 5 (for example) is completely different from each other (%11111111111111111111111111111011 vs %00000000000000000000000000000101), most of the bits get changed. Where as a floating point number, the difference is very simple (%11000000101100011100001010010000 vs %01000000101100011100001010010000), just bit 31 changes.
Sign In or Register to comment.