Saving a 'Single' data type gets corrupted when saved

This has really baffled me. Try this:

====================================

Dim s As New NotesSession

Dim db As NotesDatabase

Set db = s.CurrentDatabase

Dim doc As New NotesDocument(db)



Dim tmpNumber As Single

tmpNumber = 0.13



doc.fldNumber = tmpNumber



Call doc.Save(True, True)

====================================

Locate the newly-created document in Notes client and use the properties box to look at the list of fields.

The value being saved for the field ‘fldNumber’ is 0.129999995231628

Does anyone else get this?

When you’re writing an agent that’s doing cost calculations for a client, it’s a bit scary when numbers start changing themselves.

Can anyone tell me what’s happening?

Thanks!

Subject: Saving a ‘Single’ data type gets corrupted when saved

It is because you are using a “Single” precision floating point variable to store your number.

Because of the way that floating point numbers are stored, there are always going to be inherant accuracies. In the same way that you can’t write 1/3 as a decimal accurately.

The longer the storage area allowed to store the number, the more accurate it will be. In my experience Single is worse than useless because of these in built innacuracies.

If you change your code to:

Dim tmpNumber As Double

you should get accuracy on a par with pocket calculators, and the Notes Client is “clever” enough to hide most of these innacuracies from you.

HTH - Rufus.