## GLSL Math

Questions about the LÖVE API, installing LÖVE and other support related questions go here.
Forum rules
Before you make a thread asking for help, read this.
Ref
Party member
Posts: 690
Joined: Wed May 02, 2012 11:05 pm

### GLSL Math

Could someone provide GLSL logic?
Why doesn't:

Code: Select all

a - ((a / b) * b)

equal zero?
see line 17 of grid.lua shader.
Attachments
grid.love

raidho36
Party member
Posts: 2063
Joined: Mon Jun 17, 2013 12:00 pm

### Re: GLSL Math

Simple: foating point math has some degree of error, and it tends to accumulate along the computations.

RedHot
Citizen
Posts: 87
Joined: Mon May 27, 2013 2:43 pm
Location: Poland

### Re: GLSL Math

Raidho is wrong, as usual.

On the serious note. Your function signature is :

Code: Select all

int mod(int a, int b) { return a - ((a / b) * b); }
GLSL acts like the C language.
In C,C++, Java (that's the languages I have observed with this behaviour) an int divided by int ALWAYS return an integer.

4/3 = 1 (not 1.(3) ). That has to do with type casting. If both arguments are integers none gets promoted to a float or double, hence the return value is an integer aswell.

raidho36
Party member
Posts: 2063
Joined: Mon Jun 17, 2013 12:00 pm

### Re: GLSL Math

Previous statement wasn't wrong, just not exactly related. When people are talking about GLSL, it is assumed that any numbers are floats, so did I.

It is true that integer division always results in integer result rounded down. You can cast values at any time by prefixing them with desired cast type (or use function-like syntax):

Code: Select all

float myfloat = (float)myint
float myfloat = float(myint)
float mod ( int a, int b ) { return (float)a - ( ( (float)a / (float)b ) * (float)b ); }
Last line is what you should probably use. You can omit some of the explicit typecasts, but that only would result in implicit typecast. Implicit things may not work the way you expect it, and it's side-effects could be evil, so make sure you avoid it.

Nixola
Inner party member
Posts: 1940
Joined: Tue Dec 06, 2011 7:11 pm
Location: Italy

### Re: GLSL Math

Also, if I remember correctly some video cards just hate implicit casts and won't compile any shader that uses them
lf = love.filesystem
ls = love.sound
la = love.audio
lp = love.physics
li = love.image
lg = love.graphics

vrld
Party member
Posts: 917
Joined: Sun Apr 04, 2010 9:14 pm
Location: Germany
Contact:

### Re: GLSL Math

Even more unrelated, GLSL already defines a modulo function, see here.
I have come here to chew bubblegum and kick ass... and I'm all out of bubblegum.

hump | HC | SUIT | moonshine

RedHot
Citizen
Posts: 87
Joined: Mon May 27, 2013 2:43 pm
Location: Poland

### Re: GLSL Math

Nixola wrote:Also, if I remember correctly some video cards just hate implicit casts and won't compile any shader that uses them
Quite the opposite. A lot of ATI drivers require implcit casting as they are often reluctant to cast on their own.

@Vrld
Just my thought
Last edited by RedHot on Sat Aug 31, 2013 2:01 pm, edited 1 time in total.

Robin
The Omniscient
Posts: 6506
Joined: Fri Feb 20, 2009 4:29 pm
Location: The Netherlands
Contact:

### Re: GLSL Math

RedHot wrote:A lot of ATI drivers require implcit casting as they are often reluctant to cast on their own.
Aren't you confusing "implicit" with "explicit" there?
Help us help you: attach a .love.

RedHot
Citizen
Posts: 87
Joined: Mon May 27, 2013 2:43 pm
Location: Poland

### Re: GLSL Math

Robin wrote:
RedHot wrote:A lot of ATI drivers require implcit casting as they are often reluctant to cast on their own.
Aren't you confusing "implicit" with "explicit" there?
I most certainly am! My bad. Robin is of course right. Dunno why I confused the two.

Ref
Party member
Posts: 690
Joined: Wed May 02, 2012 11:05 pm

### Re: GLSL Math

Thanks all for your responses.
GLSL still is a black art for me.
Any and all help greatly appreciated.
[quote="raidho36"]

Code: Select all

float mod ( int a, int b ) { return (float)a - ( ( (float)a / (float)b ) * (float)b ); }
quote]
Added this to grid.lua and got:
ERROR: 0:23: ')' : syntax error parse error
Further playing around and found that I didn't need any of this code to run the script.
Don't know what the original intent was. Just part of the magic I guess.

### Who is online

Users browsing this forum: No registered users and 31 guests