Coding: Fleeting Thoughts

A place to discuss the implementation and style of computer programs.

Moderators: phlip, Moderators General, Prelates

User avatar
Xeio
Friends, Faidites, Countrymen
Posts: 5083
Joined: Wed Jul 25, 2007 11:12 am UTC
Location: C:\Users\Xeio\
Contact:

Re: Coding: Fleeting Thoughts

Postby Xeio » Sun Dec 04, 2016 9:49 pm UTC

Xenomortis wrote:
To be fair, I will definitely press Ctrl-C after a while.
But you're a program too! :shock:

User avatar
The Great Hippo
Swans ARE SHARP
Posts: 6829
Joined: Fri Dec 14, 2007 4:43 am UTC
Location: behind you

Re: Coding: Fleeting Thoughts

Postby The Great Hippo » Mon Dec 05, 2016 2:41 am UTC

I've got a data-driven entity-component system, now; you define components you want entities to have in a file that describes their default values (or the values from which they're defined):

Code: Select all

strength = 10
str_bonus = {(entity.strength - 10) / 2}
And now you can define/add those values for certain entities:

Code: Select all

>>>player = 0
>>>component.set_components(player, "str_bonus", strength=16)
>>>component["str_bonus"][player]
3
(Keywords assign a value to the component, while just providing the component's name sets the entity's component to it's default value)

Next, I'll be adding modifiers; a special dictionary inside of the component dictionary that tracks functions which intercept the final component value for an entity (like it's strength bonus) and either return the same value or a new value.

I'm already pretty certain how modifiers will work (similar to currying); the big challenge, I think, will be making them data-driven like components now are.

EDIT: The cool part about this setup -- in case it's not obvious -- is that I don't need to maintain a list of game objects; game objects are defined by what components associate with them. Aspects (groups of components with associated code) iterate over all the entities that are associated with the components that aspect incorporates.

User avatar
ahammel
My Little Cabbage
Posts: 2135
Joined: Mon Jan 30, 2012 12:46 am UTC
Location: Vancouver BC
Contact:

Re: Coding: Fleeting Thoughts

Postby ahammel » Mon Dec 12, 2016 7:17 pm UTC

MDN wrote:note that the month is 0-based
Are you fucking kidding me right now?
He/Him/His/Alex
God damn these electric sex pants!

User avatar
Thesh
Made to Fuck Dinosaurs
Posts: 5388
Joined: Tue Jan 12, 2010 1:55 am UTC
Location: Colorado

Re: Coding: Fleeting Thoughts

Postby Thesh » Mon Dec 12, 2016 7:26 pm UTC

I mean, I was going to say "Someone probably just created an enum, so just use the constants" but then I clicked the link. I don't remember that from JavaScript back in my day (which was nearly a decade ago).
Honesty replaced by greed, they gave us the reason to fight and bleed
They try to torch our faith and hope, spit at our presence and detest our goals

User avatar
Flumble
Yes Man
Posts: 1921
Joined: Sun Aug 05, 2012 9:35 pm UTC

Re: Coding: Fleeting Thoughts

Postby Flumble » Mon Dec 12, 2016 7:34 pm UTC

ahammel wrote:
MDN wrote:note that the month is 0-based
Are you fucking kidding me right now?

Well, duh.
It's been like that since C's time(.h). Or possibly even before C, but that's beyond internet history.

But it's really convenient when you want to convert a month number to a name using a 0-based array. :mrgreen:

[edit]note that all of the above is sarcasm
Last edited by Flumble on Tue Dec 13, 2016 1:14 am UTC, edited 1 time in total.

User avatar
phlip
Restorer of Worlds
Posts: 7542
Joined: Sat Sep 23, 2006 3:56 am UTC
Location: Australia
Contact:

Re: Coding: Fleeting Thoughts

Postby phlip » Mon Dec 12, 2016 10:41 pm UTC

Yeah, C's "struct tm" uses a 0-based month number but a 1-based day number. And originally it used a two-digit year, but now out of backwards compatibility it uses "year - 1900". So today is 13/11/116 according to C.

It is garbage, and every language that does the same and justifies it as "well at least it's like C" is also garbage.

Code: Select all

enum ಠ_ಠ {°□°╰=1, °Д°╰, ಠ益ಠ╰};
void ┻━┻︵​╰(ಠ_ಠ ⚠) {exit((int)⚠);}
[he/him/his]

EvanED
Posts: 4324
Joined: Mon Aug 07, 2006 6:28 am UTC
Location: Madison, WI
Contact:

Re: Coding: Fleeting Thoughts

Postby EvanED » Tue Dec 13, 2016 12:08 am UTC

phlip wrote:Yeah, C's "struct tm" uses a 0-based month number but a 1-based day number. And originally it used a two-digit year, but now out of backwards compatibility it uses "year - 1900". So today is 13/11/116 according to C.

It is garbage, and every language that does the same and justifies it as "well at least it's like C" is also garbage.
I sometimes really wish I could 'upvote' or 'like' posts on this forum... :-)

User avatar
Xanthir
My HERO!!!
Posts: 5198
Joined: Tue Feb 20, 2007 12:49 am UTC
Location: The Googleplex
Contact:

Re: Coding: Fleeting Thoughts

Postby Xanthir » Tue Dec 13, 2016 9:15 pm UTC

Javascript's Date, in particular, is a near-exact copy of Java's Date, with all the terrible mistakes that implies. Side-effect of its initial design being done in 10 days and needing a date object from the very beginning.
(defun fibs (n &optional (a 1) (b 1)) (take n (unfold '+ a b)))

User avatar
cemper93
Posts: 209
Joined: Sun Feb 20, 2011 2:35 pm UTC
Location: `pwd`

Re: Coding: Fleeting Thoughts

Postby cemper93 » Tue Dec 13, 2016 10:12 pm UTC

phlip wrote:Yeah, C's "struct tm" uses a 0-based month number but a 1-based day number. And originally it used a two-digit year, but now out of backwards compatibility it uses "year - 1900". So today is 13/11/116 according to C.

It is garbage, and every language that does the same and justifies it as "well at least it's like C" is also garbage.

Relevant rant read.

Also, general thing @TheGreatHippo: it's an interesting system you're building, but it is NOT an entity-component system, because the entire point of an entity-component system is that components do not contain logic! If you wanted to do your example of

Code: Select all

str_bonus = {(entity.strength - 10) / 2}
in an ECS, you would think about what component that formula is trying to express first, then create a component for that. Now, I don't play D&D, but if it was, say, a particular sword that gave you that strength bonus, then you would do

Code: Select all

component["particularsword"][player] = "instance of the particular sword
, and then add a system with

Code: Select all

def calculate_strength_system():
    for entity in entities:
        if entity in component["particularsword"] and entity in component["str"]:
            component["str_bonus"][entity] = (component["str"] - 10) / 2

So that's how you'd do it.

Or you, know, just do it your way. That works too. Just picking nits. :)

User avatar
ahammel
My Little Cabbage
Posts: 2135
Joined: Mon Jan 30, 2012 12:46 am UTC
Location: Vancouver BC
Contact:

Re: Coding: Fleeting Thoughts

Postby ahammel » Wed Dec 14, 2016 5:29 am UTC

Xanthir wrote:Javascript's Date, in particular, is a near-exact copy of Java's Date, with all the terrible mistakes that implies. Side-effect of its initial design being done in 10 days and needing a date object from the very beginning.

Fortunately we have better options for both languages these days. But goddamn, zero-indexed months? Why not just make the days zero-indexed while you're at it?
He/Him/His/Alex
God damn these electric sex pants!

User avatar
Xeio
Friends, Faidites, Countrymen
Posts: 5083
Joined: Wed Jul 25, 2007 11:12 am UTC
Location: C:\Users\Xeio\
Contact:

Re: Coding: Fleeting Thoughts

Postby Xeio » Wed Dec 14, 2016 4:47 pm UTC

Sometimes the one-based indexes we use for most things at work are kinda nice.

I wonder if we would use zero-based conventions if designing from scratch nowadays if there weren't so many legacy languages that had them. There's not any reason the compiler couldn't do the address translation behind the scenes for it.

User avatar
cemper93
Posts: 209
Joined: Sun Feb 20, 2011 2:35 pm UTC
Location: `pwd`

Re: Coding: Fleeting Thoughts

Postby cemper93 » Wed Dec 14, 2016 4:58 pm UTC

Xeio wrote:Sometimes the one-based indexes we use for most things at work are kinda nice.

I wonder if we would use zero-based conventions if designing from scratch nowadays if there weren't so many legacy languages that had them. There's not any reason the compiler couldn't do the address translation behind the scenes for it.

Why numbering should start at zero.

User avatar
Xeio
Friends, Faidites, Countrymen
Posts: 5083
Joined: Wed Jul 25, 2007 11:12 am UTC
Location: C:\Users\Xeio\
Contact:

Re: Coding: Fleeting Thoughts

Postby Xeio » Thu Dec 15, 2016 4:12 pm UTC

That argument seems tailored to the specific case Dijkstra chose.

So he makes the "First N numbers" subsequence case pretty:

Code: Select all

0 <= i < N
But "Starting at N length of M " becomes similarly ugly to his zero-based indices case:

Code: Select all

N-1 <= i < N+M-1
Whereas in a 1-based array the subsequence would just be:

Code: Select all

N <= i < N+M


Could be it's going over my head, but it seems like he just chose a convenient case where zero-based arrays are prettier than 1-based.

User avatar
chridd
Has a vermicelli title
Posts: 748
Joined: Tue Aug 19, 2008 10:07 am UTC
Location: ...Earth, I guess?
Contact:

Re: Coding: Fleeting Thoughts

Postby chridd » Thu Dec 15, 2016 7:32 pm UTC

Xeio wrote:But "Starting at N length of M " becomes similarly ugly to his zero-based indices case:

Code: Select all

N-1 <= i < N+M-1
Whereas in a 1-based array the subsequence would just be:

Code: Select all

N <= i < N+M
No, starting at index N length of M is N <= i < N+M in both systems. It's only N-1 <= i < N+M-1 if N is 1-based and your arrays are 0-based, and if you mix 1-based and 0-based indexing then of course you're going to have extra -1's and/or +1's.
~ chri d. d. /tʃɹɪ.di.di/ (Phonotactics, schmphonotactics) · they (for now, at least) · Forum game scores
mittfh wrote:I wish this post was very quotable...
flicky1991 wrote:In both cases the quote is "I'm being quoted too much!"

User avatar
Xenomortis
Not actually a special flower.
Posts: 1392
Joined: Thu Oct 11, 2012 8:47 am UTC

Re: Coding: Fleeting Thoughts

Postby Xenomortis » Wed Dec 21, 2016 4:40 pm UTC

Code: Select all

>>> 3 // 2
1
>>> 3.0 // 2
1.0

Python, do you really have to be like that? It's so close, but worse than not close.

Trying to maintain compatibility between Python 2.7 and 3 is a pain the arse when you don't have a proper environment for the latter set up.
Image

User avatar
Flumble
Yes Man
Posts: 1921
Joined: Sun Aug 05, 2012 9:35 pm UTC

Re: Coding: Fleeting Thoughts

Postby Flumble » Wed Dec 21, 2016 5:18 pm UTC

Who's that (//) guy and why do you want to obscure your divide-then-floor?
If types mean that much to the project, the project should be done in a stricter language, methinks.

User avatar
Xenomortis
Not actually a special flower.
Posts: 1392
Joined: Thu Oct 11, 2012 8:47 am UTC

Re: Coding: Fleeting Thoughts

Postby Xenomortis » Wed Dec 21, 2016 6:48 pm UTC

It's not me that cares about types. It's Python!

Code: Select all

>>> arr = [1, 2, 3]
>>> arr[5.0 // 2]
TypeError


Or how about:

Code: Select all

>>> (5.0 // 2) ^ 1
TypeError
Image

User avatar
Thesh
Made to Fuck Dinosaurs
Posts: 5388
Joined: Tue Jan 12, 2010 1:55 am UTC
Location: Colorado

Re: Coding: Fleeting Thoughts

Postby Thesh » Wed Dec 21, 2016 6:58 pm UTC

Easily solvable, just create your own types with their own operators whenever you want to do anything.
Honesty replaced by greed, they gave us the reason to fight and bleed
They try to torch our faith and hope, spit at our presence and detest our goals

Tub
Posts: 296
Joined: Wed Jul 27, 2011 3:13 pm UTC

Re: Coding: Fleeting Thoughts

Postby Tub » Wed Dec 21, 2016 10:38 pm UTC

Xenomortis wrote:It's not me that cares about types. It's Python!

Serves you right for using one of those half-typed languages that think they know better. Wasn't the term "duck typing" coined by the python devs? If they love duck typing so much, why don't they implement it in their language?

Code: Select all

$x = array("a","b","c");
$i = 3/2;
echo "\$x[", $i, "] = ", $x[$i], "\n";'

$x[1.5] = b

There, no fuss, it just works! If it quacks like an int, it's an int. The python devs should take notes.

User avatar
Flumble
Yes Man
Posts: 1921
Joined: Sun Aug 05, 2012 9:35 pm UTC

Re: Coding: Fleeting Thoughts

Postby Flumble » Thu Dec 22, 2016 2:20 am UTC

Xenomortis wrote:It's not me that cares about types. It's Python!

...forget I said anything. :oops:

Really? Can't access element 2.0? Nor "2" (which is totally a number because it literally says "2")?

TIL: for some reason javascript does implicitly convert integer strings to array indices, but it treats general number strings as dictionary keys.

User avatar
chridd
Has a vermicelli title
Posts: 748
Joined: Tue Aug 19, 2008 10:07 am UTC
Location: ...Earth, I guess?
Contact:

Re: Coding: Fleeting Thoughts

Postby chridd » Thu Dec 22, 2016 3:21 am UTC

Flumble wrote:TIL: for some reason javascript does implicitly convert integer strings to array indices, but it treats general number strings as dictionary keys.
JavaScript array indices are strings (numbers are converted to strings when used as indices). Objects in general are mappings from strings to values; arrays are objects whose keys happen to include "0", "1", "2", "3", etc., and "length".

In JavaScript, a[1.5] is a valid array index, the same as a["1.5"], and distinct from a[1], though assigning to a[1.5] doesn't affect the length and won't be seen by functions designed for operating on arrays. Likewise, a[-1], a[NaN], a[Infinity], a[null], and a[undefined] are all valid array elements (which, again, don't affect the length and won't be seen by functions designed for operating on arrays), because they can all be converted into strings. a[1.0] is the same as a[1] and a["1"], because 1.0 is the same as 1; a["1.0"] and a["01"] are different elements because they're distinct strings.
~ chri d. d. /tʃɹɪ.di.di/ (Phonotactics, schmphonotactics) · they (for now, at least) · Forum game scores
mittfh wrote:I wish this post was very quotable...
flicky1991 wrote:In both cases the quote is "I'm being quoted too much!"

User avatar
ahammel
My Little Cabbage
Posts: 2135
Joined: Mon Jan 30, 2012 12:46 am UTC
Location: Vancouver BC
Contact:

Re: Coding: Fleeting Thoughts

Postby ahammel » Thu Dec 22, 2016 3:56 am UTC

Flumble wrote:TIL: for some reason javascript does implicitly convert integer strings to array indices, but it treats general number strings as dictionary keys.
What's an "integer"? Nobody here but us doubles.
He/Him/His/Alex
God damn these electric sex pants!

User avatar
Flumble
Yes Man
Posts: 1921
Joined: Sun Aug 05, 2012 9:35 pm UTC

Re: Coding: Fleeting Thoughts

Postby Flumble » Thu Dec 22, 2016 1:59 pm UTC

ahammel wrote:
Flumble wrote:TIL: for some reason javascript does implicitly convert integer strings to array indices, but it treats general number strings as dictionary keys.
What's an "integer"? Nobody here but us doubles.

"integer string" being "a string containing digits only". (specifically /0|[1-9][0-9]*/ )

chridd: my quibble is that integer strings are cast to offset indices, rather than staying strings. I can't think of any other instance where strings and numbers (or other types) are allowed and JS prefers casting away from a string.

Or I'm thinking in the wrong way and the Array object is actually a hack on top of a dictionary, rather than a container on its own. I.e. it's always stored in a dictionary but there's a layer on top that includes an iterator over all keys that parse as an integer. (and possibly it is an actual array under the hood as long as it only has integer indices and is densely populated)

User avatar
chridd
Has a vermicelli title
Posts: 748
Joined: Tue Aug 19, 2008 10:07 am UTC
Location: ...Earth, I guess?
Contact:

Re: Coding: Fleeting Thoughts

Postby chridd » Thu Dec 22, 2016 4:59 pm UTC

Flumble wrote:Or I'm thinking in the wrong way and the Array object is actually a hack on top of a dictionary, rather than a container on its own.
Exactly.

Code: Select all

var a = ['a','b','c'];
for(var i in a) console.log(i, typeof i);
0 string
1 string
2 string

There's a special case that updates length if you assign to an integer string, and that deletes integer string keys if you assign length, but otherwise it's just a normal object. (Also I think recent versions of JavaScript have special rules for the order of for-in loops when you have integer-like keys, but earlier versions left the order completely unspecified.)

This is all assuming we're talking about normal arrays (Array objects, the kind you get when you use expressions like [1,2,3]); objects that look kind of like arrays but aren't might convert those strings into integers. I know String objects do (but only when getting a key that's not otherwise in the object), and I assume typed arrays do as well.
~ chri d. d. /tʃɹɪ.di.di/ (Phonotactics, schmphonotactics) · they (for now, at least) · Forum game scores
mittfh wrote:I wish this post was very quotable...
flicky1991 wrote:In both cases the quote is "I'm being quoted too much!"

EvanED
Posts: 4324
Joined: Mon Aug 07, 2006 6:28 am UTC
Location: Madison, WI
Contact:

Re: Coding: Fleeting Thoughts

Postby EvanED » Thu Dec 22, 2016 7:23 pm UTC

Not to mention:

Code: Select all

> typeof []
"object"


which I guess isn't definitive but is suggestive.

User avatar
ahammel
My Little Cabbage
Posts: 2135
Joined: Mon Jan 30, 2012 12:46 am UTC
Location: Vancouver BC
Contact:

Re: Coding: Fleeting Thoughts

Postby ahammel » Fri Dec 23, 2016 3:55 am UTC

Here's a fun talk for the C++ geeks. It's an implementation of pong in C++17 that runs on the Commodore 64 (with the help of an x86_64 to 6502 machine code converter).
He/Him/His/Alex
God damn these electric sex pants!

User avatar
phlip
Restorer of Worlds
Posts: 7542
Joined: Sat Sep 23, 2006 3:56 am UTC
Location: Australia
Contact:

Re: Coding: Fleeting Thoughts

Postby phlip » Fri Dec 23, 2016 1:40 pm UTC

EvanED wrote:Not to mention:

Code: Select all

> typeof []
"object"


which I guess isn't definitive but is suggestive.

Yeah, but typeof most things in Javascript is "object", that doesn't really narrow it down much...

Code: Select all

enum ಠ_ಠ {°□°╰=1, °Д°╰, ಠ益ಠ╰};
void ┻━┻︵​╰(ಠ_ಠ ⚠) {exit((int)⚠);}
[he/him/his]

User avatar
PM 2Ring
Posts: 3617
Joined: Mon Jan 26, 2009 3:19 pm UTC
Location: Mid north coast, NSW, Australia

Re: Coding: Fleeting Thoughts

Postby PM 2Ring » Wed Dec 28, 2016 6:40 am UTC

Xenomortis wrote:

Code: Select all

>>> 3 // 2
1
>>> 3.0 // 2
1.0

Python, do you really have to be like that? It's so close, but worse than not close.

Trying to maintain compatibility between Python 2.7 and 3 is a pain the arse when you don't have a proper environment for the latter set up.


Don't think of // as integer division: it's floor division. We got plenty of warning that the behaviour of / was going to change: the docs mentioned it way back in Python 2.2, and encouraged us to use // when we wanted floor division, and I'm pretty sure that from __future__ import division was available from Python 2.2 too.

Permitting // to take float operands lets us do neat stuff like

Code: Select all

>>> a = 1.375; a // 1, a % 1
(1.0, 0.375)
 

User avatar
chridd
Has a vermicelli title
Posts: 748
Joined: Tue Aug 19, 2008 10:07 am UTC
Location: ...Earth, I guess?
Contact:

Re: Coding: Fleeting Thoughts

Postby chridd » Wed Dec 28, 2016 6:58 am UTC

PM 2Ring wrote:Don't think of // as integer division: it's floor division.
Shouldn't floor return an integer, though (like it does in Haskell)? (At least, if you have bigints and don't have to worry about floor(1.0e308) not being representable as an int; but I'm pretty sure Python does have bigints.) I'd expect 3.0 // 2 to be acceptable (i.e., // can take float arguments) and return 1 (an integer).
~ chri d. d. /tʃɹɪ.di.di/ (Phonotactics, schmphonotactics) · they (for now, at least) · Forum game scores
mittfh wrote:I wish this post was very quotable...
flicky1991 wrote:In both cases the quote is "I'm being quoted too much!"

User avatar
PM 2Ring
Posts: 3617
Joined: Mon Jan 26, 2009 3:19 pm UTC
Location: Mid north coast, NSW, Australia

Re: Coding: Fleeting Thoughts

Postby PM 2Ring » Wed Dec 28, 2016 7:40 am UTC

chridd wrote:
PM 2Ring wrote:Don't think of // as integer division: it's floor division.
Shouldn't floor return an integer, though (like it does in Haskell)? (At least, if you have bigints and don't have to worry about floor(1.0e308) not being representable as an int; but I'm pretty sure Python does have bigints.) I'd expect 3.0 // 2 to be acceptable (i.e., // can take float arguments) and return 1 (an integer).


Perhaps. Python 2's math.floor always returns a float, but the Python 3 version returns an int if you pass it an int. In C, the floor function from <math.h> returns a double.

Python 2 uses int for machine integers and long for bigints, in Python 3 they're unified into int.

User avatar
ucim
Posts: 5435
Joined: Fri Sep 28, 2012 3:23 pm UTC
Location: The One True Thread

Re: Coding: Fleeting Thoughts

Postby ucim » Thu Dec 29, 2016 1:59 am UTC

PM 2Ring wrote:Python 2's math.floor always returns a float,
Could somebody explain to me the logic behind this? I thought the whole point of floor() was to return the lowest integer less than or equal to the argument. If floor(almost/just_about) returns 3.999999999999999999916, do we need three boats or four? I suppose we can take floor(3.999999....), getting 3.0000000000000014 (or maybe even 2.99999999999999999999142) and leave people stranded. Maybe lots of people.

Python, I expected better.

But I suppose Manuel might have programmed that section.

Jose
Order of the Sillies, Honoris Causam - bestowed by charlie_grumbles on NP 859 * OTTscar winner: Wordsmith - bestowed by yappobiscuts and the OTT on NP 1832 * Ecclesiastical Calendar of the Order of the Holy Contradiction * Please help addams if you can. She needs all of us.

User avatar
ahammel
My Little Cabbage
Posts: 2135
Joined: Mon Jan 30, 2012 12:46 am UTC
Location: Vancouver BC
Contact:

Re: Coding: Fleeting Thoughts

Postby ahammel » Thu Dec 29, 2016 2:55 am UTC

ucim wrote:
PM 2Ring wrote:Python 2's math.floor always returns a float,
Could somebody explain to me the logic behind this?
It's just a thin wrapper around C's floor function.
He/Him/His/Alex
God damn these electric sex pants!

User avatar
ucim
Posts: 5435
Joined: Fri Sep 28, 2012 3:23 pm UTC
Location: The One True Thread

Re: Coding: Fleeting Thoughts

Postby ucim » Thu Dec 29, 2016 4:27 am UTC

I guess I'm not seeing it in there. But in any case, can somebody tell me why C would return a float for the integer less than or equal to whatever? It sounds so.... wrong, and I certainly didn't expect C to do this.

Jose
Order of the Sillies, Honoris Causam - bestowed by charlie_grumbles on NP 859 * OTTscar winner: Wordsmith - bestowed by yappobiscuts and the OTT on NP 1832 * Ecclesiastical Calendar of the Order of the Holy Contradiction * Please help addams if you can. She needs all of us.

User avatar
ahammel
My Little Cabbage
Posts: 2135
Joined: Mon Jan 30, 2012 12:46 am UTC
Location: Vancouver BC
Contact:

Re: Coding: Fleeting Thoughts

Postby ahammel » Thu Dec 29, 2016 4:59 am UTC

ucim wrote:
I guess I'm not seeing it in there. But in any case, can somebody tell me why C would return a float for the integer less than or equal to whatever? It sounds so.... wrong, and I certainly didn't expect C to do this.
The relevant call is on line 992 (return math_1_to_int(number, floor, 0)). As far as I can tell, "math_1_to_int" is just going to apply "floor" to "number" and return the result (line 795). The rest looks like type-checking and boilerplate.

As for why C's floor returns a float, possibly because it's an implementation of the IEEE floating point floor function (AKA round towards −∞). That has to return a float because a) the spec says it returns a float, and b) it has to work on NaN, ∞, and -∞.
He/Him/His/Alex
God damn these electric sex pants!

User avatar
ucim
Posts: 5435
Joined: Fri Sep 28, 2012 3:23 pm UTC
Location: The One True Thread

Re: Coding: Fleeting Thoughts

Postby ucim » Thu Dec 29, 2016 5:43 am UTC

ahammel wrote:The relevant call is on line 992 (return math_1_to_int(number, floor, 0))
Yeah, I saw that, and that is just a wrapper for math_1_to_whatever().
ahammel wrote:oilerplate.

As for why C's floor returns a float, possibly because it's an implementation of the IEEE floating point floor function (AKA round towards −∞).
...and so, (I feel like a little kid asking "why?" for every answer. :) ) why would IEEE create a function whose ostensive purpose is to calculate which integer is less than or equal to.... and not actually return an integer?

Jose
Order of the Sillies, Honoris Causam - bestowed by charlie_grumbles on NP 859 * OTTscar winner: Wordsmith - bestowed by yappobiscuts and the OTT on NP 1832 * Ecclesiastical Calendar of the Order of the Holy Contradiction * Please help addams if you can. She needs all of us.

User avatar
PM 2Ring
Posts: 3617
Joined: Mon Jan 26, 2009 3:19 pm UTC
Location: Mid north coast, NSW, Australia

Re: Coding: Fleeting Thoughts

Postby PM 2Ring » Thu Dec 29, 2016 9:59 am UTC

ucim wrote:[...] why would IEEE create a function whose ostensive purpose is to calculate which integer is less than or equal to.... and not actually return an integer?


Because a machine integer is not big enough to hold all possible results of floor. Of course, double can't represent very large integers exactly either, but it's good up to about 53 bits, which is rather handy when you want to do large integer calculations on a machine that can't handle ints larger than 32 bits.

The C <math.h> functions all return double, and almost all their args are double, apart from the integer exponent args of ldexp and frexp. And when you do simple arithmetic operations with a mix of integer and floating-point operands the ints have to be converted to floating-point. Conversion between int & floating-point is fairly efficient on modern hardware, but it's not a free operation, and of course it can result in loss of significance, so it's best avoided when it's not strictly necessary.

On ancient machines that had no hardware support for floating-point such conversions had to be performed by library code, so it was even more important to avoid unnecessary conversions than it is on modern machines. But even on machines with FP hardware it's much more efficient to avoid such conversions since they hamper the efficient pipe-lining of chains of FP calculations.

User avatar
Yakk
Poster with most posts but no title.
Posts: 11045
Joined: Sat Jan 27, 2007 7:27 pm UTC
Location: E pur si muove

Re: Coding: Fleeting Thoughts

Postby Yakk » Thu Dec 29, 2016 12:20 pm UTC

The closest ieee floating point approximation of an integer is a floating point integer, not one with a bunch of .999...87 or .000..14 etc.

Floats are not random; they are mostly just integers with a power of two extra component.

There is no reason to store 7 as anything except 7 * 2^0 (and yes the encoding isn't exactly like that, but it is functionaly that).
One of the painful things about our time is that those who feel certainty are stupid, and those with any imagination and understanding are filled with doubt and indecision - BR

Last edited by JHVH on Fri Oct 23, 4004 BCE 6:17 pm, edited 6 times in total.

User avatar
ahammel
My Little Cabbage
Posts: 2135
Joined: Mon Jan 30, 2012 12:46 am UTC
Location: Vancouver BC
Contact:

Re: Coding: Fleeting Thoughts

Postby ahammel » Thu Dec 29, 2016 3:32 pm UTC

ucim wrote:
ahammel wrote:The relevant call is on line 992 (return math_1_to_int(number, floor, 0))
Yeah, I saw that, and that is just a wrapper for math_1_to_whatever().
ahammel wrote:oilerplate.

As for why C's floor returns a float, possibly because it's an implementation of the IEEE floating point floor function (AKA round towards −∞).
...and so, (I feel like a little kid asking "why?" for every answer. :) ) why would IEEE create a function whose ostensive purpose is to calculate which integer is less than or equal to.... and not actually return an integer?
Aside from the excellent reasons already mentioned:

* Because returning an integer would be inconvenient for designers of specialised floating point hardware. For that purpose, you want all the operations specified to be total functions, and to operate on floats.
* Because the purpose isn't really to calculate the nearest integer. These are rounding functions, and rounding to the nearest 0.1 is a perfectly cromulent operation.
* What integer value do you want floor(∞) to return? A programming language designer could decide to throw an exception or something, but for the purposes of the spec we want total functions, as I mentioned.
He/Him/His/Alex
God damn these electric sex pants!

User avatar
phlip
Restorer of Worlds
Posts: 7542
Joined: Sat Sep 23, 2006 3:56 am UTC
Location: Australia
Contact:

Re: Coding: Fleeting Thoughts

Postby phlip » Sun Jan 01, 2017 2:19 am UTC

ucim wrote:
[...] If floor(almost/just_about) returns 3.999999999999999999916 [...]

Then that would be a bug. floor(x) is always going to be exactly an integer.

For -21024 < x < 21024 (or thereabouts, I CBA to calculate the exact endpoints), every integer is exactly representable as a float, so you shouldn't get any rounding errors at all. Outside that range, every possible float value is an exact integer, so any rounding errors will take you to ints, not away from them (and in practise it just means that floor(x) == x, since x is always going to be integral).

So if you get a non-exact int from round(), that's not a rounding error, that's a bug.

I can't think of any numbers, even edge cases, where int(floor(x)), where floor returns a double, should give a better (or even a different) result than an intfloor(x) that rounds directly to an int. Ditto for ceil() or round().

I'll grant that having floor(x) return an int would be more useful (being able to use it as a list index, for instance) but that's just a convenience thing, not a precision thing.

Code: Select all

enum ಠ_ಠ {°□°╰=1, °Д°╰, ಠ益ಠ╰};
void ┻━┻︵​╰(ಠ_ಠ ⚠) {exit((int)⚠);}
[he/him/his]

speising
Posts: 2035
Joined: Mon Sep 03, 2012 4:54 pm UTC
Location: wien

Re: Coding: Fleeting Thoughts

Postby speising » Sun Jan 01, 2017 2:39 am UTC

Maybe even more importantly, any operation on a float has to return another float, since the value may be outside the range representable by ints.

What's intfloor(2147483649.5)?


Return to “Coding”

Who is online

Users browsing this forum: No registered users and 7 guests