Floats vs Integers

General FreeBASIC programming questions.
Post Reply
Pjbonovox
Posts: 2
Joined: Apr 22, 2019 10:01

Floats vs Integers

Post by Pjbonovox »

Hi all.

I'm sure this isn't the first time this has been mentioned, but I'm curious about floats vs integers and whether large integers would be better for 3D programming than single or double precision floats. I'm working on a project for DOS, and of course I'm trying to get as much speed as possible out of the compiler. Using this code...

Code: Select all

Dim i As Integer
Dim p As Integer
Dim q as Single
Dim r as Double
Dim t As Double

t = Timer
For i = 0 To 5000000
    p *= 2
Next
Print Timer - t


t = Timer
For i = 0 To 5000000
    q *= 2
Next
Print Timer - t

t = Timer
For i = 0 To 5000000
    r *= 2
Next
Print Timer - t
...I see these results on my 'modern' Windows machine...

Code: Select all

 0.01235959999030456
 0.01628820001496933
 0.01628129999153316
 

...and these results in DOSBox-X using 486 with FPU enabled @ 30000 cycles.

Code: Select all

0.6599998474121094
0.9900000093367432
0.9900000093367432

Almost a 50% cost. Seems like using big integers instead of floats might have a much bigger saving in that environment than I'd been lead to believe. I'm going to try it on a real piece of hardware with a x87 FPU if I can find such a thing, but just thought the results were interesting.
jj2007
Posts: 2326
Joined: Oct 23, 2016 15:28
Location: Roma, Italia
Contact:

Re: Floats vs Integers

Post by jj2007 »

Take whatever suits you, there is no speed difference. Every little line, circle or triangle you are drawing costs you thousands of cpu cycles, therefore a cycle more or less with integers instead of doubles will change absolutely nothing. I would pick floats, they are fast and sufficiently precise for graphics.
marcov
Posts: 3462
Joined: Jun 16, 2005 9:45
Location: Netherlands
Contact:

Re: Floats vs Integers

Post by marcov »

Well, the fastest is always the smallest. You can calculate 32 bytes at once using the not so uncommon AVX2. If you have not too large elements, and good simd code, usually memory movement, and not instruction count is the bottle neck, unless your calculation is very complex.

In the GPU world, many newer cards support half (which is half a single, iow a 16-bit float).
jj2007
Posts: 2326
Joined: Oct 23, 2016 15:28
Location: Roma, Italia
Contact:

Re: Floats vs Integers

Post by jj2007 »

marcov wrote:In the GPU world, many newer cards support half (which is half a single, iow a 16-bit float).
In the FPU world, you can use half integers (-32768 ... 32767, enough for most screen resolutions):

include \masm32\MasmBasic\MasmBasic.inc ; download
W1 dw 1234
W2 dw 4321

Init
fild W1
fiadd W2
Inkey Str$("W1+W2=%i", ST(0))
EndOfCode


Output: 5555
Post Reply