Mixing Data Types

Order can make a differenc when data types are mixed. For example,

1 / 2 * 3.5 = 0.0
3.5 * 1 / 2 = 1.75
3.5 / 2 = 1.75

You cannot assume that the usual mathematical laws of commutativity apply when mixing data types, especially integer and floating point types.

1.0 / 2 * 3.5 = 1.75
3.5 * 1.0 / 2 = 1.75
1 / 2.0 * 3.5 = 1.75
3.5 * 1.0 / 2.0 = 1.75


Previous | Next | Top
Last Modified September 8, 1997
Copyright 1997 Elliotte Rusty Harold
elharo@metalab.unc.edu