View Full Version : Problems negating a number

reppig

10-18-2011, 08:05 PM

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))

Phil Pilgrim (PhiPi)

10-18-2011, 08:10 PM

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 Fwed

10-18-2011, 08:44 PM

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).

reppig

10-18-2011, 09:09 PM

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??

reppig

10-18-2011, 09:10 PM

I will give it a try tommorrow. Thanks.

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).

reppig

10-18-2011, 09:11 PM

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)

10-18-2011, 09:14 PM

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 Fwed

10-18-2011, 09:33 PM

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.