Show HN: I wanted to measure CO2 in my office, so I build a C++ WebServer/App - <AUTHOR> https://github.com/Maddimax/MadCO2 <comment><USER2> Shameless plug: I wanted to measure PM10 and PM2.5 pollution level in my neighbourhood, so I built the ESP8266-based sensor using instructions from Luftdaten [1] and since the original interface is pretty ugly, I created my own UI:

[http://smog.rekawek.eu/](http://smog.rekawek.eu/) [https://github.com/trekawek/air-quality- info](https://github.com/trekawek/air-quality-info)

(I know, it's PHP, but can be hosted virtually anywhere!)

[1] [https://luftdaten.info/feinstaubsensor- bauen/](https://luftdaten.info/feinstaubsensor-bauen/)

<comment><USER3> I built a pretty similar rig around an RPi Zero W, same SDS011 AQ sensor and BME280 temp/pressure/humidity sensors as Luftdaten, just polled by a couple of python scripts on cronjobs.

</comment></comment><comment><USER4> I would have just had the CO2 level read via a $8 I2C device connected to an Esp8266/Esp32 running Arduino.

I would post the result to a web service in this fashion from the Esp8266:

[https://techtutorialsx.com/2016/07/21/esp8266-post- requests/](https://techtutorialsx.com/2016/07/21/esp8266-post-requests/)

Then just setup a nice Graphana+Prometheus as a service, graphana.com is nice and can expose a Prometheus end point over basic http auth - easy to post to. Then you can setup alerts, graphs, dashboards, etc.

In fact you could have a whole suite of IoT reports flowing in from various Arduinos really easily. Or at least a couple CO2 sensors spread around the house.

Total investment of coding would be about 30-50 lines of Arduino code for the Esp8266 and you may even get away with the free plan on Graphana.com.

I obviously like to outsource as much as possible these days. Less learning, but more efficient.

<comment><USER5> Would you be able to share where you might get such a cheap CO2 sensor/the name of it? I've had trouble finding them for less than £60 in the UK and would like to set one up.

<comment><USER6> [https://www.co2meter.com/collections/sensors](https://www.co2meter.com/collections/sensors) has a bunch of different ones, I usually look on Mouser also. I don't have a name of one currently. I've been using the particle.io platform with a particle device/LTE for sending out data and programming them.

<comment><USER7> None of them are in the 8$ range. There also seem to some copies which are way overpriced.

<comment><USER6> Interesting to have an arbitrary $8 price point. You only live once, get the best!

<comment><USER7> Not my restriction.

</comment></comment></comment></comment></comment></comment><comment><USER8> Installed a CO2 monitor in my home two days ago. Already changed my life. It warns me when I need additional ventilation and even beeps if CO2 levels raise too high.

It sounds silly but it's scary how quickly levels rise when the windows are closed.

<comment><USER9> > _It sounds silly but it 's scary how quickly levels rise when the windows > are closed._

It might be scary, but is it actually dangerous? It could just be that the levels shown, even if raised, are inconsequential.

Basically wondering about the "Already changed my life" line.

Are we all without one risk certain CO2 death every day, or is the parent overly worried over nothing (and opens windows without really a need for it).

<comment><USER10> Higher ppm decreases cognitive function even at pretty low levels, so being alerted to open your window might improve your work / study.

There's at least this study: [https://ehp.niehs.nih.gov/doi/10.1289/ehp.1510037](https://ehp.niehs.nih.gov/doi/10.1289/ehp.1510037)

And this video has the quote "At 1000ppm there is a 15% decrease in cognitive function". [https://www.youtube.com/watch?v=1Nh_vxpycEA](https://www.youtube.com/watch?v=1Nh_vxpycEA)

<comment><USER11> _" At 1000ppm there is a 15% decrease in cognitive function"_

In a century or so we could be looking at similar CO2 levels in the atmosphere if we don't significantly cut emissions.

Will our decencents be made stupider by higher CO2 levels? Or will they be able to adapt?

<comment><USER12> My wife is a respiratory therapist. When asked, she said higher-than-average CO2 is seen in COPD patients, and is compensated through an increase in metabolic bicarbonate production, countering the acidic properties of CO2. We are still entering unknown territories with such large populations getting exposed to higher CO2 levels, but she thinks we'll be fine.

<comment><USER13> I'm having trouble recounciling that with the study that shows cognitive decline, can you explain your wife's thinking?

<comment><USER14> Maybe it means that if you are _always_ exposed to high CO2 your body adjusts, but if you are sporadically exposed it does not? Kind of like living in the mountains vs going there occasionally

<comment><USER15> >> adjusts

Only true if adjustment isn't giving up higher functioning of the body for a less efficient metabolism.

<comment><USER16> Fortunately, we're really good at compensating for blood acidification. It's offset with higher bicarbonate levels and more excretion, which means the reaction uses carbonic acid and hydrogen to offset carbonic acid - pretty low- consequence to produce. Having more bicarbonate raises calcium and potassium levels slightly, but even when it's way outside of any normal exposure it doesn't seem to cause further effects.

</comment></comment></comment></comment></comment></comment></comment></comment></comment><comment><USER17> This device's manufacturer claims an accuracy of "± 100ppm or ± 7% of reading".

But there's a review on Amazon that describes someone getting three units and finding as much as 600ppm variation among them.

Maybe they need some kind of calibration or tuning? Or their manufacturing process is way out of control.

<comment><USER18> They have a calibration feature, of course most of the reviews fail to mention if the person understood how it worked and used it. (I personally find the mechanism a bit odd, but I guess it makes some sense if you assume some things about the environment)

</comment></comment><comment><USER19> I'm looking around for an affordable household CO2 measurement tool but they're all wildly expensive. Is this technology complicated and cannot be obtained for $30? They're all like $120+

Edit: I realised that this is a _perfect_ item for public libraries to loan out. Expensive, only needed occasionally, easy to use, potential for significant positive public impact on becoming more informed and healthier.

If you're looking for a cause that's affordable, practical, and local, consider buying and donating one to your local library.

<comment><USER7> MH-Z14 or Z19. But this is just the sensor, you'll need an Arduino/ESP/Rpi too.

<comment><USER20> What's particular about these sensors? I've been researching technologies to monitor plant growth and haven't yet looked into CO2 sensors, hence my asking. Do they come in an IC package?

<comment><USER7> They use optical measurement, are reasonable in pricing. Outputs are UART, PWM and analoge.

They have pins with 2.54mm spacing if that helps you.

<comment><USER20> Would you use something else if you meant to solder something on a PCB for medium scale production?

<comment><USER7> Don't know. There are chemical sensor with a heating element, but those usually suck.

</comment></comment></comment></comment></comment></comment><comment><USER21> No story behind the build, just a link to the git repo? yay

<comment><AUTHOR> You can only put link + title on here. And the story is rather boring :) I wanted to measure it, I got a device from amazon, found a python script to read the data, the dependencies (pyhidapi) always sigfaulted, so I wrote my own :)

<comment><USER22> Why do you need a script for this? Isn't the device good enough at telling you the bare necessary information?

Still a cool script nonetheless, no hate.

<comment><AUTHOR> Sure, but I wanted to see the data over time. And I was bored :)

</comment></comment></comment></comment><comment><USER13> Sounds like you have a ventilation problem, buildings are required by code to get a certain amount of fresh air from outside so that CO2, VOCs, chloroform, etc don't get too high.

Incidentally, NHTSA projects in 2100 CO2 will be at 789ppm [0] in the atmosphere, up from 410 today and 350 pre-industrial. And there are higher projections. One would assume that this would make ventilation more urgent, I guess building codes will have to be updated and existing buildings retrofitted.

[0] [https://www.washingtonpost.com/national/health- science/trump...](https://www.washingtonpost.com/national/health- science/trump-administration-sees-a-7-degree-rise-in-global-temperatures- by-2100/2018/09/27/b9c6fada-bb45-11e8-bdc0-90f81cc58c5d_story.html)

</comment><comment><USER23> Whats wrong with this one? (it works with the same device) [https://github.com/larsp/co2monitor](https://github.com/larsp/co2monitor)

<comment><AUTHOR> That looks good as well! I actually didn't know about that project. It wouldn't have worked on my specific case though since I wanted it connected to my Mac, that tool only works on Linux.

<comment><USER24> The Mac doesn't provide a device to read from? Because beyond that, it should be pretty similar (posix-ish and golang)?

<comment><AUTHOR> Yeah, you have to use IOKit, which hidapi wraps.

</comment></comment></comment></comment><comment><USER25> Anyone know other, possibly cheaper, devices besides [https://www.co2meter.com/products/co2mini-co2-indoor-air- qua...](https://www.co2meter.com/products/co2mini-co2-indoor-air-quality- monitor) that can display current CO2?

I hoped there would be cheaper versions on Aliexpress, but no luck :(

<comment><USER26> MH-Z19 is a sensor on sale on Aliexpress for ~20 USD. It works with Arduino and ESP32

</comment></comment><comment><USER27> Why are there steps in the CO2 levels?

<comment><USER28> Since this is based on a reverse-engineered binary protocol and since the step sizes are reasonably close to 64 and 128, I'd guess it's a bug.

<comment><USER29> It looks like there's no need to reverse-engineer the protocol:

[https://www.co2meter.com/products/co2mini-co2-indoor-air- qua...](https://www.co2meter.com/products/co2mini-co2-indoor-air-quality- monitor)

</comment></comment></comment><comment><USER30> Unrelated to the project, but your CO2 levels are far to high. The maximum should be around 600ppm.

<comment><AUTHOR> Yes, I agree. I've already ordered a bunch of plants :) Sadly my building is quite old and ventilation is poor.

<comment><USER31> I heard that you would need about 40(?) plants per person. Best advice was to be well ventilated unless the outdoor CO2 is worse than indoors.

<comment><USER32> You literally can't have enough plants in the house to compensate for the CO2 production of its inhabitants.

You would have to place water immersed algae farms on every open space in the house and keep them illuminated with artificial light 24/7, because plants will produce CO2 at night otherwise.

Like you said, good ventilation is far more effective than any plant.

</comment></comment></comment></comment><comment><USER33> I wanted to do this in my office and was told, "if we find out something is wrong then we'd have to fix it." _facepalm_

<comment><USER34> I think it's safe to say CO2 levels in your office are already over the limit with coworkers making arguments like that lol

The company saved $25 though!

</comment></comment><comment><USER35> I am new to this one. I always wonder about accuracy. I think it depends on where you place your sensor in the room matters. If it is in place where lot of window air is hitting, it might alter the values. Or am I completely wrong? Or are you using multiple devices to get the average of those devices?

<comment><AUTHOR> Yes, absolutely. I only have one device at the moment, but it sure would be interesting to see how fast the levels equalize between rooms

</comment></comment><comment><USER36> Friend of stackmad here. Just wanted to let you know that he got himself a plant as a result of the CO2 measurements :)

<comment><AUTHOR> Actually its 9 plants ;)

</comment></comment><comment><USER37> Architecturally speaking this should be a super simple script that pulls the sensor data into a database for any use instead of a custom written http server that manages the sensor.

<comment><AUTHOR> Yes, I agree. But this was way more fun :)

</comment></comment><comment><USER38> Isn't C++ a bit overkill for a web app?

<comment><USER39> You are right, C++ is overkill. It would be much better if it was a javascript application with thousands of dependencies.

<comment><USER40> ahh so you're in the camp of wanting to re-create a graphing library for every project..

<comment><USER41> ahh so you're in the camp of outsourcing your development cost to the user's hardware.

</comment></comment></comment></comment><comment><USER42> I've been using the Microsoft cpprestsdk, too, but for client stuff. I've been enjoying using it.

<comment><AUTHOR> Yeah, its quite ok.

</comment></comment><comment><USER43> that thermostat is working pretty well, keeping the temperature stable like that

<comment><AUTHOR> Yeah, I've finally managed to tweak the thermostat on the radiators and the main thermostat for the heater to where I like it :)

</comment></comment><comment><USER44> I'd improve the ticks on the x-Axis a bit. Otherwise looks fine to me.

<comment><AUTHOR> Yeah, that was pretty quickly hacked together :)

</comment></comment><comment><USER45> @dhh highly recommends Awair for air quality monitoring. He gave some talk on it after be bought one for his whole staff (can't find the talk for some reason)

I got one and it's pretty cool. A bit of a game to get a high score.

</comment><comment><USER46> what is the baseline for CO2 on certain Sq.ft?

<comment><AUTHOR> I guess it should match the outside air as close as possible. So around 400ppm

</comment></comment><comment><USER47> I believe the title should be "I wanted to build a C++ WebServer/App, so I built a C++ WebServer/App".

Not that there anything wrong with that :)

<comment><AUTHOR> :) But the CO2 measurement part honestly came first.</comment></comment>


A new wave of videogames offers lessons in powerlessness, scarcity and failure - <AUTHOR> http://aeon.co/magazine/culture/welcome-to-the-scarcity-games/? <comment><USER2> Another attractive element of these games is the moral questions that sometimes arise. I know this is particularly true in "This War of Mine" (e.g. - choosing to take in a starving child that comes knocking on your door, but who also becomes a resource burden on your small tribe).

Questioning and reflecting on decision making, moral choice, and consequences of (in)action are a central focus of literature study, so I would love to know about additional games in this vein that I could potentially bring into my classroom as 'game-texts'.

Suggestions?

<comment><USER3> "Moral" choices seem to be a trend lately, especially with BioWare RPGs and their ilk (Dragon Age, Mass Effect). Unfortunately, most of the consequences are fairly minor, mostly only revolving around the opinions of your in-game acquaintances. The events unfold a little differently sometimes, but the end result is almost always the same.

The worst of it is that they always main-line you into one moral extreme or the other. It's always obvious which choice is "good" and which is "bad", and once you've made that choice for the first time you'll always make the same choice later on, else you'll miss out on the in-game benefits of having a "pure" moral stance. Mass Effect and Infamous especially do this.

<comment><USER2> This has been my experience as well. I am attracted to bringing games into the learning environment because they can offer unique opportunities for action/reaction/reflection (in addition to other benefits) - but I am indeed more interested in complicated decision making rather than simply choosing Red Team v. Blue Team.

For instance, I would love to see a game like "Papers Please" with a stronger element of consequences. Rather than simply being told you allowed a terrorist through the gates (with a brief cut-away) the consequences of this action (deaths, social destabilization, etc.) would impact the future game play. Perhaps, you decide to actively aid and abet these terrorists (by communicating to them that you will allow them and their compatriots to pass through) - but then you put yourself and your loved ones at risk, as well as increase social instability.

This is already a wonderful game, but I would like to see the premise taken even farther. Games could be a wonderful place to consider and critique decision making and moral choices that are analogous to those we face in real life (even if filtered through clearly fictitious events/circumstances).

<comment><USER3> To the opposite extreme, there's also The Stanley Parable. On the surface it's a humorous narrative seemingly about being a cubicle drone, but it's ultimately about the futility of choice.

<comment><USER4> One could argue that Bioshock Infinite is about the same thing.

<comment><USER3> I liked that, but none of the player's choices purported to be very important, either.

<comment><USER5> Bioshock Infinite doesn't purport to give the player more than the standard illusion of choice, but I think it's more interesting how choice is handled in the narrative.

The characters think that they're making big, important decisions that reshape their world. It turns out that they weren't really.

</comment></comment></comment></comment></comment></comment></comment><comment><USER6> I think another influence that the article doesn't touch on much are games like Amnesia: The Dark Descent. Horror games in which you have no weapons and can't fight back against the monsters; your only defense is being terrified and running away and hiding, and the resources you do have (in Amnesia, light sources) are very limited.

</comment><comment><USER7> > These games subvert the usual arc of heroic triumph by providing a basis for > interesting, beautiful defeat.

I was half-expecting a nod to Dwarf Fortress.

<comment><USER8> Embark. Mass confusion. Eaten by gators.

There's nothing anybody could have done... Poor dwarves.

</comment></comment><comment><USER9> Good. I am so bored of the whole power trip trope which is pervading most video games. At this point, ennemies would have to trip on my sword in order to make some games easier. Arcade games tend to be 'artificially unfair' in order to make sure that the player will die and have to pay to continue. Many modern mainstream games take the opposite approach and make sure that the average game will feel like a demigod. It is not without merits, especially with a hero battling against oversized ennemies. For example, it is very gratifying to fight enemies 10 times as big as the protagonist of Darksiders. I feel like it is reaching a dead end though. Parkour / scaling in Assassin Creed is a one button affair. You just look at your hero do these feats. Platforming is equally easy in that game series, you just have to find the designed path thought up by the developers and press the right button, which is even conveniently displayed on the screen. Ezio is able to take on dozen of guards and I usually flee only because I am bored of the easy kills. Skyrim goes out of its way to make you feel like 'yet another chosen one', and judging from the game quest, you are litteraly the only person able to get anything done in that world. Not to mention that very soon you become a walking demigod able to kill anything from liches to dragons. I can't shake the feeling that these games are very condescending to the player. They avoid giving the player a real challenge in order to make sure that everybody can feel powerful with minimal effort.

I don't think that games have to be as brutal as Dark Souls or even unfair like Banished. Avoiding the 'suddenly all powerful chosen one' trope would be a good start. Please let me play a random schmuck that is not going to defeat all the demons of Hell and save the world all by himself.

</comment><comment><USER10> The author is making a big point of banished being a game of inevitable demise, but that's not how I found it.

To me, it's a game of hope and industriousness. Where he sees starving workers staving off inevitable death, I see villagers keen to make their new settlement work.

I think there's a lot of room for interpretation both ways, which is great for the games, but it means this article is quite shallow and offers no real insight, because it's trying to build on a foundation that is viewed differently depending on who you are.

</comment><comment><USER11> This reminded me of Avalon Hill's _Outdoor Survival_. It was recommended for use as an outdoor "dungeon" in the old boxed D & D. Adventuring outdoors was kinda dull for us as kids, compared to indoor slaughter but we did play the actual board game fairly often.

Dying of thirst was not uncommon.

</comment><comment><USER12> is taboola now posting to hackernews?

</comment><comment><USER13> A long time ago (though admittedly in this galaxy) I made a computer game with moral choices a key element in the gameplay. I named it Legacy. It was a Java GUI app. Though a small game in terms of feature set and code size I always remember noticing how it was one of the more fun (to me) games I ever made. Had to do with mating, family and inheritances. It ended up influencing my more recent designs as well.

<comment><USER2> Is it available to play?

<comment><USER13> the code exists on one of my disks somewhere. If you email me your email address I could notify you when I find it and get it usable for you. my email in my profile.</comment></comment></comment>


Show HN: binb, guess the song and prove your music knowledge - <AUTHOR> http://binb.jit.su/

<comment><USER2> i'll need to match the title and the artist 100% accurate, which is pretty insane, considering that you play against the time.

I entered "Next Episode" and it gave me errors, because i didn't write "The Next Episode"

<comment><AUTHOR> Two typos are allowed. Leading "The" is optional on artist name but required on title.</comment></comment>


Ask HN: How to Promote/Market Mac Apps - <AUTHOR>

I just recently launched my first mac app to the Mac App Store (http://https://itunes.apple.com/us/app/mackernews-hacker-news-client/id946730699?mt <comment><AUTHOR> Lol so yeah so didn't paste my comment properly here. Basically wondering what Mac app devs are doing regarding this (Google/Twitter/FB ) ads?</comment>


IDI Has Built a Profile on Every American Adult - <AUTHOR> http://www.bloomberg.com/news/articles/2016-08-05/this-company-has-built-a-profile-on-every-american-adult?cmpid=BBD080516_BIZ <comment><USER2> None of this clickbait nonsense compares to what Facebook and Google have on most of us and the average American. These people that run something like this and boast about knowing "When the last time you had Chinese food was" are literally the information industry's equivalent of click farms and scraper sites.

<comment><USER3> Are Facebook and Google selling granular personal information to third parties and individuals on specific people by request?

I fully acknowledge they utilize the information they mine about you to optimize their ad networks, but that's quite a bit different from releasing the information for a price.

<comment> o0-0o Yes, they are. You can sign up for retargeting information from them, and cross reference that with those people when they visit your site. You pay it forward and pay a bit per segment, but you get the same results.

<comment><USER3> Retargeting doesn't release the information to you. They are selling _access_ to their network under specific conditions. Retargeting just lets me say, "make sure X sees my ad", it doesn't let me say "I know of X person, send me all their information so I can do what I want with it."

There's a pretty big difference. Facebook and Google would not be in business if they let third parties aggregate their detailed personal records.

</comment></comment></comment></comment><comment><USER4> If you are unaware, Americans can get copies of their records from Lexis- Nexis, who runs a very similar business. You get one free copy annually under the FACT act.

Mine ran to over 60 pages.

[https://personalreports.lexisnexis.com/access_your_full_file...](https://personalreports.lexisnexis.com/access_your_full_file_disclosure.jsp)

<comment><USER5> Ha. So the "Full File Disclosure" form has entries for full name, dob, social security, driver's license info current address, past addresses, phone number. If they somehow didn't have that info before, they would definitely have it after you send the form it. Also kind if a neat trick, much like the coupon trick from the article.

It would be funny if you sent in the form and then if you weren't in the system, they'd just photocopy the form and send it back to you with a "thank you" note.

Funny side story about them: I tried to interview there once, many years ago. They have this antiquated in-house developed query language -- a skill you couldn't transfer anywhere. So you'd sort of be stuck there, for life.

Up-front said would force me to works on weekends, for what already was looking like a miserable pay. As if living in Dayton, Ohio wouldn't have been enough punishment.

Best part was the future manager made a joke about how she likes to "crack her whip sometimes", which made the other engineer in the room laugh ... nervously. Was half expecting him pull me to the side and whisper "escape while you still can".

<comment><USER4> Wow, bragging _in an interview_ about riding employees hard is special. OTOH, you should be grateful! That may have saved you from an awful situation. (But yeah, I bet they're currently whining about an engineering shortage.)

Anyhow, I didn't provide my ssn, just drivers license plus my current address on a utility bill. I'm sure CA has already sold them the drivers license data anyway.

</comment></comment></comment><comment><USER6> > Dubner declined to provide a demo of idiCORE or furnish the company's report > on me.

Hmm.

<comment><USER7> The entire article reads like a PR piece. There's nothing newsworthy there, it's just an ad for the company.

<comment><USER8> Completely agree. When I scanned the article I thought, 'bull shit.'

There are a lot of players in this space, and everyone that I've evaluated so far is basically selling astrology (i.e. profiles that could match 60% of the attributes of any sample of the population).

</comment></comment></comment><comment><USER9> "<Company name> built a profile on every American adult" is less clickbaity than "This company has built a profile on every American adult."

I wish people tried harder to reduce click bait titles.

<comment><USER10>
      I wish people tried harder to reduce click bait titles.

So do I, but do they have incentives to do so? Can we maybe think of ways to incentivize them?

<comment><USER9> The mods are our only hope.

</comment></comment></comment><comment><USER11> The fact that they are running coupon websites to actively 'link' profiles together was surprising, kind of ingenious.

<comment><USER12> That's what they get from buying Fluent. They actively take in consumer data from all kinds of sources and marketing campaigns as well as sell data, and traffic for those purposes. Fluent does a lot of co-registration path stuff such as can be seen here [http://www.nationalconsumercenter.com/flow.aspx](http://www.nationalconsumercenter.com/flow.aspx) and run sites like [http://www.sweepstakesaday.com](http://www.sweepstakesaday.com) and [http://www.getsamplesnow.com](http://www.getsamplesnow.com)

Then you have Q Interactive which they also bought which was largely known for clickgen.com and coolsavings.com Like Fluent they are big in co-registration, lead generation, data collection & sales, affiliate marketing etc.

That's really a tiny bit of the data that gets traded on consumers every day. All kinds of transactions get logged and sold. Credit card declined transactions are an example that surprises many people however it's the kind of data that debt consolidation, pre-paid debit card and credit management companies love.

</comment></comment><comment><USER13> Just a baby. Acxiom has been doing this for 40+ years.

</comment><comment><USER14> Reinsurance companies (which backstop pretty much all insurance in the US) also do this, with significantly more private data available. I would imagine there are quite a few companies that try this.

<comment><USER15> Why reinsurance? What's in it for them versus consumer insurance companies?

<comment><USER16> Consumer insurers all utilize re-insurers. They essentially loan out the money to the insurance companies, they are the ones really taking on all the risk so they get all the underwriting data from the insurance companies.

<comment><USER15> The reinsurers give a shit about profiles about individual policyholders on the consumer level? I just can't see how that level of effort makes sense on that scale.

<comment><USER14> They are the most sophisticated in the industry. They also handle something like 7T in assets.

The insurance companies themselves are fairly empty pass-thru companies for a lot policy types. They all use the same forms, call centers, models, brokers. So the reinsurance companies make sure the models are good.

</comment></comment></comment></comment></comment><comment><USER17> The interesting part is not that these databases exist; rather, it's a question of what happens when they're leaked online.

</comment><comment><USER18> This is why I do odd stuff like decline supermarket club cards, I'd wager the profile they have on me is quite limited.

</comment><comment><USER19> Is there not an equivalent to the UK's Data Protection Act in the States?

</comment><comment><USER5> > Dubner says most Americans have little to fear. As examples, he cites > idiCORE uses such as locating a missing person and nabbing a fraud or > terrorism suspect.

Right. Think of the children and the terrorists.

<comment><USER20> At this stage government must declare everyone a criminal, terrorist and child abuser and let people work hard and pay taxes to get their names off the list by the time they die.

<comment><USER5> Even better, by acting irrationaly it exposes itself to abuse by those who know how to manipulate those labels.

Some neighbor doesn't like how you cut the grass -- calls the tip line telling them he saw you build bombs in your basement. Your house is raided, yet no one will be punished because they can claim "But I was thinking of our country and safety".</comment></comment></comment>


Microsoft spins Android malware into Windows Phone giveaway - <AUTHOR> http://www.computerworld.com/s/article/9222631/Microsoft_spins_Android_malware_into_Windows_Phone_giveaway?taxonomyId=77 <comment><USER2> My first thought was typical Microsoft FUD, but at least they're not alone. Apple has used the virus card often enough. (<http://www.apple.com/why-mac/>)</comment>


Sorting - We're Doing It Wrong - <AUTHOR> http://blog.rodneyrehm.de/archives/14-Sorting-Were-Doing-It-Wrong.html

<comment><USER2> Maybe I'm the only one who missed the memo, but in case anyone else is wondering what a hash is doing in JS:

"When writing (in text, not in JavaScript) about properties on specific prototypes, you may use the JavaScript notation, foo.prototype.bar. However, this shorter (non-JavaScript) notation is often used instead: foo#bar. Basically, the hash (#) stands for .prototype. — fancy huh?"

<http://mathiasbynens.be/notes/javascript-prototype-notation>

<comment><USER3> A bit confusing since it's a common convention to use # to refer to instance members, e.g. Foo#bar means my_instantiation_of_foo.bar

<comment><USER4> That's exactly what happens in the JavaScript case:

      new Array().sort === Array.prototype.sort # true

</comment></comment></comment><comment><USER4> This is fixed in Python:

The sort() method takes optional arguments for controlling the comparisons.

"key" specifies a function of one argument that is used to extract a comparison key from each list element.

In general, the key and reverse conversion processes are much faster than specifying an equivalent cmp function. This is because cmp is called multiple times for each list element while key and reverse touch each element only once.

<http://docs.python.org/library/stdtypes.html>

</comment><comment><USER5> For a moment, I thought there was actually something new about sorting. That turned out not to be the case, of course.

I understand the importance of linkbait titles, but specifying the context (e.g. "we're doing it wrong in JavaScript") would save people time without weakening the impact on those who might be interested in the article (JavaScript developers).

<comment><USER6> Thank you. HN seems to be subject to tons of vague or mildly misleading titles and I'm getting pretty sick of it.

I wouldn't be bothered as much, but in some places if you get into too many technical details it seems to confound people and then frustrate them. This is kind of silly since I'm still in college and probably one of the least educated coders here.

Seems wrong to be then downvoted for using basic technical knowledge in a place called Hacker News.

</comment></comment><comment><USER7> This is a very cool visualiser that was posted on HN before if anyone feels like playing: <http://visualsort.appspot.com/>

<comment><USER8> previous discussion: <http://news.ycombinator.org/item?id=3040157>

</comment></comment><comment><USER9> This is worth reading for that video alone. Mergesort was particularly cool.

</comment><comment><USER10> javascript sorting: having expected a sorting somewhat similar to python and being very... surprised that javascript essentially sorts everything but primitive types by comparing their String counterparts, I decided to implement a more sensible compare function, similar to e.g. python:

<https://gist.github.com/2772234>

I am currently using this in server only environments, so there is no comparison for DOM elements.

</comment><comment><USER11> Wow-- apparently Mergesort can be implemented in-place!?

<comment><USER12> Yes, but people usually don't since it requires more swap operations and is pretty hard to get correct.

<comment><USER13> what, "hard to get correct"? Are we talking about fundamental computer science that literally consists of implementing an algorithm made out of compare and swap/move elements...?

I can't imagine any sort algorithm for which it can possibly be legitimate to say it's 'hard to get correct' unless it's in the sentence 'it's hard for a first-year computer science student to get correct.'

i mean, what is more traditional and basic than implementing tricky sort algorithms?

<comment><USER14> How about binary search?

"When Jon Bentley assigned it as a problem in a course for professional programmers, he found that an astounding ninety percent failed to code a binary search correctly after several hours of working on it,[7] and another study shows that accurate code for it is only found in five out of twenty textbooks.[8] Furthermore, Bentley's own implementation of binary search, published in his 1986 book Programming Pearls, contains an error that remained undetected for over twenty years.[9]"

[http://en.wikipedia.org/wiki/Binary_search_algorithm#Impleme...](http://en.wikipedia.org/wiki/Binary_search_algorithm#Implementation_issues)

<comment><USER15> I just learned that Java has an unsigned right shift operator >>> from an article cited as a reference in that Wikipedia section. The article has some more details on the subtleties of binary search. For instance, the 20 year old bug in an algorithm proven to be correct was down to integer ranges:

      int mid = (low + high)/2;

breaks (obviously in retrospect, am I missing something?) for

      low + high > Integer.MAX_INTEGER

It can be replaced by the elegant (but to me, somewhat oblique)

      int mid = (low + high) >>> 1;

[http://googleresearch.blogspot.de/2006/06/extra-extra- read-a...](http://googleresearch.blogspot.de/2006/06/extra-extra-read-all- about-it-nearly.html)

Also from that page, here's the link to the bug in Sun's tracker, priority 2-High, evaluation: _"Can't even compute average of two ints" is pretty embarrassing._

<http://bugs.sun.com/bugdatabase/view_bug.do?bug_id=5045582>

</comment></comment></comment></comment></comment><comment><USER16> Another great example of Javascript being awful.

<comment><USER17> Care to elaborate?</comment></comment>


Dennis Ritchie passes away at 70 (2011) - <AUTHOR> http://techcrunch.com/2011/10/13/father-of-c-and-unix-dennis-ritchie-passes-away-at-age-70/

<comment><USER2> Assuming the point is that we focus too much on big personality rather than actual contribution, this is something that bugs me and I think about often. Ken Thompson is still around. So is Bjarne Stroustrup. Linus Torvalds is still heading the linux project. We need to celebrate these people. There are a ton of them and even as an enthusiast, I don't know who they all are and I'm ashamed. Most regular people haven't even heard these names and probably don't care to. Hell Stallman probably deserves a nobel prize for GNU.

<comment><USER3> Every industry has those people, though. With seven billion people in the world, there are going to be a _lot_ of incredibly amazing people that we will never hear of, just because the bandwidth it would take to even hear their names (much less really understand their contributions) is just far too great.

Even though there are millions of outstanding contributors to our societies and technologies whose names I will never know, I am grateful for them.

</comment></comment><comment><USER4> This would have been much more poignant had:

1) This been posted on October 12th

2) We were still discussing the anniversary of Jobs' death on that date.

As it is right now, of course people are discussing the death of Jobs and not Ritchie _because people don't usually talk about the 358th day since someone's death_.

</comment><comment><USER5> I think the submitter is making the point that we seem to have forgotten about Ritchie but have managed to remember the one-year anniversary of Steve Jobs's death.

<comment><USER6> That's fine, but I don't recall seeing any of the recent Jobs anniversary posts worded in the present tense. The title is just confusing. I was well aware of it at the time, but I had to think for a second because something in my brain was telling me that someone _else_ had died, since Ritchie had passed away a year ago. I feared for Ken Thompson for a moment there.

[Edit: the date was not in the title originally as it is now.]

<comment><USER5> I think people occasionally submit present-tense articles for past events to try to make some very specific kind of point, sort of like a "have you forgotten?".

It's a bit bewildering and I'm not totally sure I understand the point.

The one that comes to mind recently was a 6 September 2012 submission with headline "Swedish rape warrant for Wikileaks' Assange cancelled" linking to the article <http://www.bbc.co.uk/news/world-europe-11049316> from 2010.

<comment><USER7> It's lazy and passive-aggressive, the implication being, "figure out why this is relevant."

</comment></comment></comment></comment><comment><USER8> Someone made this comment on Techcrunch and it sums up the impact of Ritchie's work:

Without Ritchie, we would not have iOS for the iPhones, the Mac operating system and no basis for Java within the Android phones. No basis for Python and Ruby since they are written in C. Linux stood on the shoulders of Unix, just as Steve Jobs/Apple stood on the shoulder of many giants and this man was one of those giants. We lost a great tech leader. His impact was huge!, as billions of dollars was made releasing products that benefit from his work but he wasn't a billionaire, but impact is more than money.

</comment><comment><USER9> Just in case you're confused, this article was published on October 13, 2011.

<comment><USER10> Yeah, that's what I thought.

</comment></comment><comment><USER11>
      free(DennisRitchie); /* If ptr is a null pointer, no action occurs. */

</comment><comment><USER6> In 2011.</comment>


Idea for killer mobile app? - <AUTHOR> http://battellemedia.com/archives/004623.php

<comment><USER2> These are people who JUST DON'T GET IT. You see, texting is a completely different form of communication.

<comment><USER3> Exactly.

Moreover there are reasons why texting evolved as the popular application of the underlying technology _SMS_ , which supports many other forms of broadcasts. (including the killer idea)

Excerpt from Wikipedia

 _Most thought of SMS as providing a means to alert the individual mobile user, for example, of a deposited voice mail, whereas others had more sophisticated applications in their minds, such as telemetry. However, few believed that SMS would be used as a means for sending text messages from one mobile user to another._

Read on - <http://en.wikipedia.org/wiki/Short_message_service>

<comment><USER2> Exactly. The guy writing the article misunderstands why people send messages with text. It has little to do with convenience, or because there is no alternative to it - it's just a form of communication that many people like for whatever reason. I won't make the same mistake and claim that I know why others prefer texting sometimes, but personally, I use it when I want to make a neutral query to someone, when I want to communicate with someone without revealing the tone of my voice, when I want to give a person the opportunity to ignore what I am saying or to delay a reply, etc.

Communication is wide and varied, and SMS is a choice in communication. Trying to replace SMS with voice is like saying that one should replace email with telephone calls. The applications are different, and people use it for a different _type_ of communication.

<comment><USER4> texting is a form of asynchronous communication who places the greater burden to the sender who has to spend more time typing a message, while a phone call can be faster.

But, text messaging is considerate to the receiver, as it lets the receiver consume the message at their own time, while a call would have to interrupt the person whatever they are doing.

Text is better in very noisy environments, bars/clubs/concerts.

In Europe at least, texting as always been a little bit cheaper than a phone call, and the receiver usually doesn't pay for received messages.

<comment><USER2> But it's more than just convenience. It's also the impersonal part of the text that makes it so popular. You can write stuff in text messages that would be wierd saying out loud.

Just like me writing this message to you - I can think about what I am writing, revise it, etc.

</comment></comment></comment></comment></comment><comment><USER5> Already done. And successful enough to be acquired. Yet by no means 'killer'.

Rapid Messaging Service (RMS): <http://www.heyanita.com>

</comment><comment><USER6> I think Jott already does it as a premium service-- but who wants to call a flippin phone number just to send a quick txt?! I've _heard_ there are some people who swear by jott, but I think they are all old people because if you are halfway competent with a smartphone it just seems like a pain in the ass.

You could try to port <http://www.speech.cs.cmu.edu/pocketsphinx/> to the iphone...

Or even easier just stream the audio off to a sphinx-4 server and push the txt results back to the phone in near-realtime for visual confirmation/correction and sending. Very seamless for the end user and compensates for voice recognition sketchy.

It would be interesting to then run the text through some kind of txt-speak filter that would shrink the text. I'm always impressed by people who can cram a small novel into 140 characters.

</comment><comment><USER7> Voice recognition would obv be very cool if it was instant and involed machine tralsnation so you could talk in english and on the other end it comes out in chinese(or whatever language you choose).

Also, some phones already have voice recognition, if minimal, right? You can say a name and the appropriate person is called.

<comment><USER8> That is (as far as I know) only voice-matching. It matches the name you said to whatever pre-recorded read in name you have assigned to a contact.

So it's not like it actually understands what you say, more that this input "pretty much" matches this pre-recorded file.

There's a long way to from there to full, usable voice-recognition.

</comment></comment><comment><USER8> So basically he thinks a killer idea is to 1. use a phone, 2. to do voice recognition, 3. to send message as text, 4. to recipient which somehow will get the same message read as if spoken originally.

So what he is saying that every phone should have voice recognition, voice regeneration and blah blah. Colour me stupid, but for this purpose, why not, you know, send a voiceclip over MMS or just make a damn call?

This is a hammer looking for a nail.</comment>


Polynomial time solution for an NPC problem on a quantum computer - <AUTHOR> http://arxiv.org/pdf/1206.4747.pdf

<comment><AUTHOR> has anyone who knows what they're talking about looked this over? if its correct, it seems like its a pretty huge result</comment>


Password Manager - <AUTHOR>

Does anyone know a good password manager that we can use in our startup? We would prefer something open-source and that let us share the passwords. <comment><USER2> Keepass (<http://keepass.info>) KeepassX, a multi-platform Qt port (<http://www.keepassx.org>) Password Safe (<http://passwordsafe.sourceforge.net>)

All three of these write to a single database file. This means that sharing the database would require having file available via a network share, _or_ keeping it in version control.

</comment><comment><USER3> This topic comes up all the time. You might want to do a search and sift through some of the other popular threads.

</comment><comment><USER4> I use KeePass, and keep it in sync it with different machines with Dropbox.

<comment><USER5> That works for personal use, but when sharing between multiple people KeePass will get locked up when multiple people have the application open at the same time (despite warnings people _will_ keep it open for long periods of time). This means you can't write to the DB and can lead to corrupted databases. Its more work, but we moved to using Git to manage our Keepass DB.</comment></comment>


Using Mahout to Group Religious Twitter Users - <AUTHOR> http://technicalelvis.com/blog/2013/03/28/mahout-twitter-1/

<comment><USER2> Strange, but interesting, and a good introduction to clustering.</comment>


IoC containers, a comparison - <AUTHOR> http://codetuple.com/articles/aspnet/HQMvY32Nzxp/ioc-containers-a-comparison-1 <comment><USER2> I have used Mef before! Worked out pretty well.</comment>


Diversity in Tech: Why It Hasn't Happened and How to Change That - <AUTHOR> https://medium.com/@lukefilipos/diversity-in-tech-why-it-hasnt-happened-and-how-to-change-that-394b3b7fbf3f <comment><USER2> This seems much broader than diversity, it's really just an industry problem.</comment>


Man flying a jet pack spotted at LAX - <AUTHOR> https://www.cnn.com/2020/09/01/us/jetpack-lax-trnd/index.html <comment><USER2> Popular discussion on this yesterday:

[https://news.ycombinator.com/item?id=24341535](https://news.ycombinator.com/item?id=24341535)</comment>


Using floating-point numbers for money - <AUTHOR> https://www.evanjones.ca/floating-point-money.html <comment><USER2> Sorry, but I remain skeptical. The same people who use `float` for financial calculations are probably the same people who don't understand how/why to do the rounding described to avoid these problems. Way too many programmers think that they are working in base 10 when using float. Why not just make them use it, and keep them out of trouble.

Also, I wonder what the overhead of rounding every operation is? Comparable to the cost of using a proper Decimal class?

"if you are doing some financial math that does not need to be accurate to the penny, just use floating point numbers."

I would argue that financial math by definition needs to be accurate to the penny. Where is "pretty close" financial calculations considered acceptable? Having worked at a bank, I know how seriously this sort of thing is taken.

From experience, working in scientific applications and numerical computing, summing large numbers of floats is fraught with accuracy problems too.

<comment><USER3> > I wonder what the overhead of rounding every operation is? Comparable to the > cost of using a proper Decimal class?

For what it's worth, for the major ICs (Intel, NVIDIA, etc.) there is zero extra overhead. A choice of rounding modes is part of the floating point operation's instruction. And keep in mind that a floating point op is _always_ rounding no matter what you do, the question is whether it's always using the same rounding strategy consistently, whether you can control it, and whether it has what you need.

> I remain skeptical

You are correct.

There are good reasons not to use float for money that the article didn't discuss, and perhaps the author isn't aware of. You run out of integer precision at 2^24, which is only 16 million. If you process a 20 million dollar payment in units of dollars, you might be off by at least a dollar. That error will multiply with every floating point op done on the result. If your units are pennies, the largest safe value is only 160,000 dollars. If you ever subtract floats, like say make a payment or withdrawal, you can run into catastrophic cancellation without knowing it. Deposit $200k and then withdraw $199k, suddenly you have a small balance with large error that could remain in your account and continue to grow until the balance is zero. [https://pharr.org/matt/blog/2019/11/03/difference-of- floats....](https://pharr.org/matt/blog/2019/11/03/difference-of-floats.html)

<comment><USER4> It has great benefits to the user, though. Once you have $20 million in the bank, you can continue to withdraw $1 at a time as often as you like without spending any of your principal!

<comment><USER5> By benefits to the user which would mean the bank made a mistake and now the IT department has a production P0 issue that will probably have everyone working to fix that yesterday then sure.

</comment></comment></comment></comment><comment><USER6> No, do not do financial calculations in binary floating point. Converting between base 2 and base 10 fractions can do funny things (including during the rounding step, which is also 99% guaranteed to be inaccurate because base 10 fractions can't be exactly represented as base 2 fractions).

Most professional financial packages use fixed point decimal, which can easily be implemented by specifying a fractional unit (such as thousandths of cents). The computations will be faster (because they're ints) and at 64 bits you get a min/max value of +- 9,223,372,036,854,775,807 units (90 quadrillion if you're using thousandths of cents), and rounding functions will always be exact (provided you have enough digits of scratch precision).

There's also the 2008 addition of decimal floating point types to ieee754 [1], which have been implemented in gcc and clang (software emulation only).

So no, don't use binary floats for financial calculations. You're 99.9% guaranteed to get it wrong and introduce bugs.

[1] [https://en.wikipedia.org/wiki/Decimal64_floating- point_forma...](https://en.wikipedia.org/wiki/Decimal64_floating-point_format)

<comment><USER7> A notable exception to the general lack of floating decimal support is C#, which has had floating decimal (System.Decimal) support in the CLR from the start (128 bits with a scale of 0 to 28).

[https://docs.microsoft.com/en-us/dotnet/csharp/language- refe...](https://docs.microsoft.com/en-us/dotnet/csharp/language- reference/language-specification/types#the-decimal-type)

<comment><USER8> Apple SDKs have NSDecimal [0] and NSDecimalNumber [1].

[0]: [https://developer.apple.com/documentation/foundation/nsdecim...](https://developer.apple.com/documentation/foundation/nsdecimal)

[1]: [https://developer.apple.com/documentation/foundation/nsdecim...](https://developer.apple.com/documentation/foundation/nsdecimalnumber?language=objc)

</comment></comment></comment><comment><USER8> The author links to an article about how Excel does all its computations in 64-bit floating point, in a way that seems to bolster their point. The actual title of that article? "Floating-point arithmetic may give inaccurate results in Excel" ([https://docs.microsoft.com/en- us/office/troubleshoot/excel/f...](https://docs.microsoft.com/en- us/office/troubleshoot/excel/floating-point-arithmetic-inaccurate-result)).

How coincidental that this should show up on the front page at the same time as the thread on normalization of deviance in software: [https://news.ycombinator.com/item?id=22144330](https://news.ycombinator.com/item?id=22144330)

What is wrong with using a currency library?

<comment><USER9> > What is wrong with using a currency library?

Do currency libraries exist for example for GPUs? (Maybe they do.)

<comment><USER10> If you need your financial code to run in the GPU either you don't need fixed point number (simulations) or your code is extremely inefficient.

<comment><USER9> > you don't need fixed point number

That was my point.

</comment></comment></comment></comment><comment><USER11> Of course you _can_ , but for the vast majority of cases, you _shouldn't_. Floating points aren't designed to solve the problems financial calculations bring, they're designed for general purpose math and efficiency.

If you're programming a point-of-sale system, or a ecommerce site or something, using IEEE-754 floats would be madness. The increase in performance compared to decimal types is absolutely infinitesimal, and the cost of getting it wrong is MASSIVE. Just don't do it!

Of course, if you really do need the performance advantage floats offer (e.g. high-frequency trading or machine learning), then by all means, use floats. But that's a tiny minority of those programmers who have to deal with money.

<comment><USER12> How would you suggest to store and calculate things like taxes? For example in NYC the retail sales tax is 8.735%. Obviously the final amount could be stored as an integer in cents, but I'm talking about the tax rate and the calculations of it. I guess you need to calculate the tax for each item, round it to cents (and I assume the rules on how you round can vary by jurisdiction, so it's not just calling your language's round function), multiply that by the quantity of that item (good luck if the quantity can be a decimal value), then sum each line item to get the total tax.

<comment><USER13> Easy, either implement a decimal type (4 fractional digits) as most programming languages have them implemented by users. or multiply by 10000 then do your calculation and then divide by 10000 again. I used a decimal type for the administration program I coded.

<comment><USER14> > _Easy, either implement a decimal type_

Great, how do you become confident that your hand-rolled decimal type is less buggy than the known hazards of using floating-point?

<comment><USER15> If you've never done something like this, you might want to try it. It's a nice simple programming exercise and the basic version, with tests, will take less than an evening.

</comment></comment></comment></comment></comment><comment><USER16> This is not great advice, and he defeats his own argument without realizing it. The main problem with this article is the damn title, which is absolutely awful advice. The body of the article ultimately contradicts the title. Anyone who didn't read the article but read the title might be more likely to use floating point numbers for money now.

> One piece of popular programming wisdom is "never using floating-point > numbers for money." This has never made sense to me.

He then goes on to explain why it absolutely does and must make sense. It honestly seems like the author's entire point is about not accepting blanket statements.

> My summary: if you are doing some financial math that does not need to be > accurate to the penny, just use floating point numbers.

Arguably all financial math needs to be accurate to the penny. I can't help but think he is maliciously trolling, trying to convince people of something that isn't true.

</comment><comment><USER17> From the summary: "if you are doing some financial math that does not need to be accurate to the penny"...

Of course, if you can accept rounding errors, it is fine. But in many monetary applications, that is not OK. The article is quite pointless.

Edit. For example, when doing budget forecasts, the result will never be accurate to the penny. It is then OK. But when doing accounting, you need to account for every penny. Not OK.

</comment><comment><USER18> Disclaimer: 30+ year veteran at fixing these fucking bugs.

My opinion: just NO.

MULTIPLE failures to understand the problem have occurred in this article, and they deal with _representation_.

A) It is never acceptable to 'round up' on interest payments/loans/debts/balances, without having an explicit line item for why. "Because compiler decision made by programmer" is indefensible.

B) Representation is EVERYTHING. Case in point:

>>>>0.1 + 0.2: Produces 0.30000000000000004 but should be exactly 0.3.

This statement can kill.

C) Standards are there to keep everyone, and everything, 'honest', and by honest it means: everyone understands things implicitly. Explain to grandma why a rounding error has resulted in her teeth falling out, or GTFO.

D) Fixed point is a standard _representation_. Its not about the precision- fail, its about if everyone gets exactly the same results, every time, in doing the math.

E) Lots and lots of these bugs yet to be fixed.

<comment><USER19> Generally, is the penny indivisible in finance? (So is using integers fine for everything, as long as you remember you you are dealing with cents, not dollars)?

</comment></comment><comment><USER20> > Solution: Round after every operation

No, the solution is not to use floating point numbers to store money.

I worked on an iOS app in fintech for years, and let me assure you, using floating point numbers to calculate currency is an exercise in frustration and lack of correctness. When balances are wrong you're losing your customers money, which in turn loses trust in your product. You know it's inexact, an approximation, and your customers want an exact answer.

Just do it with a fixed-precision decimal number, and represent it as a string.

<comment><USER21> > Just do it with a fixed-precision decimal number, and represent it as a > string.

Why? If you are going to do that, might as well use an integer of cents. It's more compact and if you're worried about these calculations and storing this kind of information, it's likely you are storing lots of it (or it wouldn't be a problem).

<comment><USER20> It'd need to be integer cents plus a scale factor which determines the smallest unit of currency. Japan has a scale factor of 0 as compared to the US 2. A string incorporates this conveniently. Either is fine.

</comment></comment></comment><comment><USER22> Yes you _can_ but that's how you get bugs.

I used to work in the online gambling industry for 15 years and there are a large number of use cases on a gambling front end where you do financial arithmetic.

I have seen equality between 2 floats being used as setting the end condition for a count up win animation. I don't think I have to explain what horrible thing that lead to.

</comment><comment><USER15> There's more to money than what happens in the checkout line, and that's where the problems with floating-point math arises. For example, one codebase I'm aware of ran into serious problems with line item refunds when currency conversion was involved, IIRC. ("Serious" as in "couldn't generate a refund that exactly matched the charge on the customer's original invoice.")

<comment><USER23> Exactly, the results are not reversible if you apply rounding.

</comment></comment><comment><USER24> The funny thing is that if you're dealing with (US Treasury) bonds, (EDIT: binary) floats are actually kind-of the right thing, since they trade in 32nds of a dollar (and binary fractions thereof)[1][2]

> Unlike U.S. equity markets, which switched to decimal pricing in 2001, U.S. > Treasury securities still trade in fractions. In particular, prices are > quoted in 32nds of a point, where a point equals one percent of par, with > the 32nds themselves split into fractions. On the BrokerTec platform, for > example, 3- and 5-year notes trade in quarters of 32nds, whereas 7-, 10-, > and 30-year securities trade in halves of 32nds. The quoted price for a > 5-year note might be 98-15¼, for example, indicating a price in decimal form > of 98.4765625 (that is, 98 + 15⁄32 + ¼⁄32).

[1] [https://www.newyorkfed.org/aboutthefed/fedpoint/fed07.html](https://www.newyorkfed.org/aboutthefed/fedpoint/fed07.html)

[2] [https://www.bloomberg.com/opinion/articles/2020-01-15/it- s-n...](https://www.bloomberg.com/opinion/articles/2020-01-15/it-s-not- insider-trading-if-the-president-does-it)

<comment><USER25> Unfortunately it's 32nds of a 100th, so they still don't work in floats (because floats are stored normalised, as 1.x in binary).

Eg. 117.125

<comment><USER24> Is it? The NY Fed site says it's 32nds of a dollar:

> • Prices are quoted in 32nds of a dollar.

> [...] Note and bond prices are quoted in dollars and fractions of a dollar. > By market convention, the normal fraction used for Treasury security prices > is 1/32.

<comment><USER25> Right, but the price is about 100, not 1.

If you had a price of 1.25 then you could represent this as a float as (binary) 1.01. However if you have 117.25 then that's (decimal) 1.83203125 * 2^-6. You can see how you quickly run out of bits.

</comment></comment></comment></comment><comment><USER26> From the article: "I am not a theoretician and have not proven that this is actually correct."

So, yes, you might get away with floating point values for financials -- and I have even seen banking code that did -- but that doesn't mean it's a good idea. Especially when libraries providing fixed point and arbitrary precision decimal representations are widely available and easy to use.

The biggest problem with using IEE 754 for currencies is that IEE 754 defines a domain and a set of operations that don't match the domain and set of operations required for currency calculations. The discrepancies between the "right" answers and the answers given by IEE 754 aren't because IEE 754 is in some way "wrong", it's because it's not calculating what you think it's calculating.

<comment><USER27> I once had to send a bug report to a big bank, like Bank of America, because their JavaScript front end wasn't rounding and got the typical 0.1+0.2>0.3 error (or whatever). This meant that I couldn't submit a dispute online and had to do it on the phone. Like most companies, they don't bother thanking you, or letting you know if it's fixed.

It's great how coding style books rarely address these important things and focus only on bookshedding. Anyone know of a good presubmit checklist for semantic issues?

</comment></comment><comment><USER28> I think this is kind of funny. Using floats and rounding at some fixed precision before rounding to penny precision is basically exactly the same as using integer multiples of whatever you're first rounding the float to. So you're basically not using floating point anymore, you're just using a float type to represent fixed point integers. The problem as I see it here is that you need to be careful to round to the fixed precision at the right places. The easiest way to not miss a place is to do it after every operation, and the best way to do that is to abstract your money type as a class. So now what we're comparing is a fixed point class holding a float vs an int. In my opinion, holding an int is the better option, because it's slightly more obvious when the values overflow the maximum range of that integer than when the precision of the float drops below your fixed point rounding delta. In either case you need to add some error handling and I also prefer branching on ints than floats (mostly because of a big perf difference on the CPU I used to work on).

</comment><comment><USER29> You can, but you shouldn't. Use of fixed-point numbers is way more reliable, often faster as it's implemented with integer numbers, you can easily detect overflow, you can increase/decrease precision if you need, etc.

</comment><comment><USER30> So this article's advice is to use rounding, just after he showed an example where rounding didn't work. Also if you forget to round once you can let errors accumulate.

> if you are doing some financial math that does not need to be accurate to > the penny

There is no such thing. I worked with applications that dealt with tens of billions of Euros, and if the bottom line was off by even 1 cent, the users would come to us to figure out what went wrong. Suggesting that this is acceptable to knowingly introduce errors, when there is a pretty well- established practice that allows to avoid them entirely, is baffling.

</comment><comment><USER31> > Since we "know" the exact answers have a finite number of decimal digits, we > can just round off the lower part of the numbers, which will produce the > nearest float with that number of digits.

What does that even mean, "produce the nearest float with that number of digits?"

If my float representation says e. g., 2.6749999999999998, and I "tell" it to round to three digits, the result is still 2.6749999999999998 in floating point representation, isn't it?

A floating point number doesn't care about the number of significant decimal digits you want it to have.

<comment><USER32> Good floating-point printing functions output the shortest possible representation. So if 2.675 and 2.6749999999999998 are indistinguishable, print(2.6749999999999998) will show 2.675

<comment><USER31> My impression was that the article is about intermediate rounding between calculation steps, rather than about printing a number to the screen.

<comment><USER24> As far as (64-bit binary) floats are concerned, 2.675 and 2.6749999999999998 are in fact the same thing:

        $ python3
        Python 3.7.3 (default, Jun 16 2019, 16:10:46)
        [Clang 8.0.0 (clang-800.0.42.1)] on darwin
        Type "help", "copyright", "credits" or "license" for more information.
        >>> 2.675 == 2.6749999999999998
        True

So IF you have enough excess precision at every step, and IF you do the rounding right at each step, then you can get the same results using binary fractions as with decimal.

Of course, correct rounding is still an issue when using a decimal type, since round(sum(n for n in ns)) ≠ sum(round(n) for n in ns).

</comment></comment></comment></comment><comment><USER33> Disagree with this one - problem is that floating point errors tend to be cumulative and doing something simple like summing a year of sales records the errors start to reflect in pennies surprising quickly. But luckily this has been a solved problem for decades - fixed point and binary coded decimal.

[https://en.m.wikipedia.org/wiki/Fixed- point_arithmetic](https://en.m.wikipedia.org/wiki/Fixed-point_arithmetic)

[https://en.m.wikipedia.org/wiki/Binary- coded_decimal](https://en.m.wikipedia.org/wiki/Binary-coded_decimal)

</comment><comment><USER34> I ran into a weird problem this week trying to convert a string "8.95" to an integer (8.95) that made me question if even Int is good enough and as suggested in the comments here, use BigInt or equivalent for anything to do with numbers.

This also happens with Javascript and Elixir, not just Ruby. So perhaps it has something to do with how the cpu stores the numbers?

I know this isn't StackOverflow, but could someone shed some light on to what's happening here? [https://ideone.com/1U7rgf](https://ideone.com/1U7rgf)

<comment><USER8> The problem here isn't integers at all, as 8.95 is not an integer but an integer (8) plus a fraction (0.95 or 95/100==19/20). That's in decimal, but since computers use binary, your decimal fraction must be converted to a binary fraction. This is what floating point is used for, although it's important to know that floating point is an approximation of the real number line, and also that fractions that are rational in decimal may be irrational in binary, namely if the decimal denominator is not a power of two.

Looks like that's what's going on with your example. Floating point approximation of an otherwise unrepresentable irrational binary which resulted in that repeating sequence when converted back to decimal again. I don't know how BigDecimal works but I guess just by the name it is not floating point but rationals represented by arbitrary precision integer numerators and denominators.

Definitely read up on how floating point representation works, with binary mantissas and exponents, normalization, and converting decimal fractions to binary fractions.

<comment><USER35> > That's in decimal, but since computers use binary

Well, if you tell them to, e.g., by using a binary floating point type instead of a decimal type.

<comment><USER8> No, we aren't using computers with logic gates that have 10 discrete states. Even if you're programming to decimal, the underlying representation is still binary. But there are numerical differences between using decimals represented by rationals with binary integer numerators and denominators, vs floating point with binary mantissa and exponent.

<comment><USER35> > No, we aren't using computers with logic gates that have 10 discrete states. > Even if you're programming to decimal, the underlying representation is > still binary.

That is not the sense of "computers use binary" that justifies the conclusions in the sentence in which the phrase was used in the post being responded to, so while true on its own, in context it is an example of the fallacy of equivocation.

<comment><USER8> Could you help me understand where I've gone wrong here? I think there's some miscommunication going on.

<comment><USER36> Are you familiar with binary coded decimal? Decimal format floats are in somewhat the same vein.

See, for instance, [https://en.m.wikipedia.org/wiki/Decimal32_floating- point_for...](https://en.m.wikipedia.org/wiki/Decimal32_floating-point_format)

<comment><USER8> This is basically what I meant when I said "computers use binary"

<comment><USER36> That sense is trivially true, but it doesn't follow that you have binary fractions.

<comment><USER8> Sorry, but I'm not parsing this sentence:

> it doesn't follow that you have binary fractions

Is that in reference to something I said earlier, or are we just falling deeper down a rabbit hole of hair splitting?

The point I was driving at is that a given number we think about in decimal like 12.3456 can be represented in binary in more than one way, one of which are rationals backed by integers for num/den, another is floating point with mantissa and exponent. Which way is chosen ultimately affects the outcomes of computations. I was trying to explain why OP was seeing unexpected results. I don't think I've been mistaken in the explanations...

<comment><USER36> The issue is that you are missing the possibility of decimal floating point, which is also defined in IEEE 754 but which is typically not exposed as a primitive in programming languages. Imagine a BCD mantissa. That _can_ encode 12.3456 exactly.

So it's not "since computers use binary", but "since we use binary floating point". There are fair arguments that we made that choice because of efficiency concerns driven by the fact that "computers use binary", but I don't think that really helps the explanation.

Note that the original objection was picking at a nit that I am not sure I would have picked at, I am just seeking to clarify.

</comment></comment></comment></comment></comment></comment></comment></comment></comment></comment></comment><comment><USER37> Why do so many languages not have a decimal type as a primitive value? Is it legacy? Is it because a decimal type must fundamentally be an object that wraps some other primitive values?

<comment><USER23> Looks like Go v2 might get decimal floating point numbers support: [https://github.com/golang/go/issues/19787](https://github.com/golang/go/issues/19787)

</comment></comment><comment><USER38> > My summary: if you are doing some financial math that does not need to be > accurate to the penny, just use floating point numbers.

So... Don't use floats where accuracy is important? I agree.

</comment><comment><USER39> > 0.1 + 0.2: Produces 0.30000000000000004

No, it doesn't. It produces 0.3000000000000000444089209850062616169452667236328125.

> 1.40 * 165: produces 230.99999999999997

No, it doesn't. It produces 230.999999999999971578290569595992565155029296875.

> round(2.675, 2) produces 2.67

No, it doesn't. It produces 2.6699999999999999289457264239899814128875732421875.

> This is because 2.675 is actually 2.6749999999999998

No, it isn't. For one, 2.675 obviously is not 2.6749999999999998, but it also isn't converted to 2.6749999999999998 to be stored as a floating point number, it is converted to 2.67499999999999982236431605997495353221893310546875.

> round(2.665, 2) which produces 2.67

No, it doesn't. It produces 2.6699999999999999289457264239899814128875732421875.

> However, in floating-point numbers, it is above the halfway point > (0.00500000000000000097)

Well, it is above the halfway point, but it's actually 0.0050000000000000009714451465470119728706777095794677734375.

> I am not a theoretician and have not proven that this is actually correct.

So, all the numbers are wrong, you have no clue whether your wrong results generalize, but you use that as the basis for advice to other people? I only can hope that you don't write software for other people to use.

</comment><comment><USER40> Isn't it just the difference between measuring and counting?

There's no occurrence of the word count here [https://en.m.wikipedia.org/wiki/Accuracy_and_precision](https://en.m.wikipedia.org/wiki/Accuracy_and_precision).

We can count things and know that number exactly. We cannot measure anything to exactness.

</comment><comment> scott-smith_us Another consideration:

We've all probably read that "Programs must be written for people to read, and only incidentally for machines to execute."

When another (maintenance) developer sees float/double being used to represent money, their first reaction is going to be "uh oh, this dev didn't know what s/he was doing". There's a big risk of someone reflexively refactoring this later on.

You may ask, "What if I put in big comment blocks saying essentially 'I knew what I was doing here'?"

Well, even if maintenance devs read that and believe you, they're still likely to introduce errors, since they aren't used to using floats for money, and don't know all the tricks to avoid errors...

Unless there's some justifiable win in size / performance, this (although interesting) falls under my "Don't be too clever" rule.

</comment><comment><USER41> >We can solve these problems by rounding after every operation.

In my experience one should do exactly the opposite, round/format only the very last result when you need to print it, otherwise always keep all the decimals in the intermediary results to avoid accumulating rounding errors as in a Superman 3 bank hack (aka "salami slicing"). I did a lot of affiliate and e-commerce software in my time and if you're careful you can use floats (sometimes you have to deal with old systems working that way), but fixed- point calculations with integers are way easier and safer. Just make sure to store enough extra decimal places, not just the 2 that you print and you can calculate interests, exchange currencies, charge 0.5% fee on $0.01 per click transactions, whatever - it works just fine.

</comment><comment><USER42> A lot of financial firms use an integer of the smallest indivisible unit of currency wherever possible (stripe is one). This has the handy property that string and numeric representations are identical so no risk of being flipped into scientific notation by some layer. That happens more often than you'd think.

FX is a different world. Spot FX trading systems are fully automated and compute rates to 5 DP. That was 15 years ago, I don't know if it's more now. Large Yen amounts can come worryingly close to the limits of precision.

I once watched a colleague trying to persuade a roomful of people to stop using floats. The demo showed what happens to 0.1 + 0.1 + 0.1. The answer was not 0.3. I'm not sure they believed him. Some of them are probably still writing software for your bank!

<comment><USER43> For shits and giggles:

        $ python3
        Python 3.7.4 (default, Sep  7 2019, 18:27:02)
        [Clang 10.0.1 (clang-1001.0.46.4)] on darwin
        Type "help", "copyright", "credits" or "license" for more information.
        >>> print (0.1 + 0.1 + 0.1)
        0.30000000000000004

        $ irb
        2.5.1 :001 > print 0.1 + 0.1 + 0.1
        0.30000000000000004 => nil

        Chrome browser console:
        > 0.1 + 0.1 + 0.1
        < 0.30000000000000004

        $ php
        <?php
        echo 0.1 + 0.1 + 0.1;
        0.3

<comment><USER44> Is this because PHP is correct, or is it because PHP rounds reprs at a different level of precision?

</comment></comment></comment><comment><USER45> I once worked at a place that had a third party vendor create a reporting front-end to the outdated minicomputer that the business ran on. After I was there a couple years, the CFO said "It's really weird, the numbers we get out of the reporting system are close, but never exactly right when we compare them by hand to the system."

This person was seriously thinking "this is just how computers are", and after a day of looking into it I was able to determine all the issues they were facing were floating point problems when the Windows front-end did it's calculations. It was pulling all the data directly from the old system, after all. How could it be different?

</comment><comment><USER23> Aside from rounding I have 2 reasons to not use them where (financial) precision is necessary:

\- [https://developer.mozilla.org/en- US/docs/Web/JavaScript/Refe...](https://developer.mozilla.org/en- US/docs/Web/JavaScript/Reference/Global_Objects/Number/MAX_SAFE_INTEGER) (it's the same in other languages)

\- if results are not exactly reversible then you cannot implement verification and that hinders defensive programming

</comment><comment><USER46> Floating point numbers are numbers in scientific notation for computers. I don't see people without computers doing finance calculations in scientific notation. It's not natural and error happens because of this.

Floating point numbers were not created to represent numbers which could be non-integers. They were created to represent numbers in scientific notation. Very useful for physics and scientific calculations, but not in finance domains.

</comment><comment><USER47> > A 64-bit floating-point number can represent 15 decimal digits, which is all > balances less than 10 trillion (9 999 999 999 999.99), with two digits after > the decimal place

There are real monetary quantities for which this is insufficient. The GDP of Indonesia is measured in 10s of quadrillions of Rupiah. In cases of hyperinflation this kind of overflow can happen even for human-level accounting.

Use real fixed point decimal math.

</comment><comment><USER48> Others have mentioned most of the pitfalls with the suggested approach.

Another potential issue is that the floating point rounding mode can be modified by shared libraries or plugins.

[0] has examples of application crashes caused by a change in the rounding mode.

[0] [http://www.virtualdub.org/blog/pivot/entry.php?id=53](http://www.virtualdub.org/blog/pivot/entry.php?id=53)

</comment><comment><USER49> The hidden "gotcha" in most spreadsheets is that formatting cells to currency or 2 decimals changes only the display, not the number itself. To do that and eliminate potential floating point errors, wrapping EVERY currency formula in one of the round functions is standard practice for bookkeeping...

</comment><comment><USER50> We've already had this argument on stackoverflow _10 years ago._

[https://stackoverflow.com/questions/582797/should-you- choose...](https://stackoverflow.com/questions/582797/should-you-choose-the- money-or-decimalx-y-datatypes-in-sql-server)

</comment><comment><USER51> No, please don't. Often you have to combine money with quantities that also need unit of measure conversions and this just is one step too far. Frankly, chasing the error goes from a programming problem to a contract problem very quickly.

Also, you are going to end up storing in decimal fields in the database anyway.

<comment><USER52> > Also, you are going to end up storing in decimal fields in the database > anyway.

why not use cents as the unit and store it without decimals?

<comment><USER51> Because it is a massive pain in the butt for your report writers / business intelligence people whose tools expect decimal fields. Never mind all the existing code that expects an actual decimal and not an integer. Plus tax folks like like their mills.

<comment><USER21> > Because it is a massive pain in the butt for your report writers / business > intelligence people whose tools expect decimal fields

That's a matter for the ETL that dumps the data on their screens.

<comment><USER51> Yeah, no. First, sometimes you actually don't get to use a ETL before doing the report, and massaging an integer or float to decimal in an ETL is just going to cause you trouble.

</comment></comment></comment></comment></comment><comment><USER53> $ python3 Python 3.7.3 (default, Jun 16 2019, 16:10:46) [Clang 8.0.0 (clang-800.0.42.1)] on darwin Type "help", "copyright", "credits" or "license" for more information. >>> 2.675 == 2.6749999999999998 True

</comment><comment><USER54> Yeah isn't this just a real bad idea? There's no 0.1 in half/single/double precision arithmetic for one. Is "speed of financial calculations" really the bottleneck for these apps? I kind of doubt it.

</comment><comment><USER55> Large financial calculations where errors accumulate are more often than not done in database queries, not in the application software.

What is faster in Postgresql? Double floats and TRUNC after each operation or using NUMERIC(precision, scale)?

<comment><USER56> hey bro, where's your scholarly record of impactful publications?

</comment></comment><comment><USER57> I worked for a large financal services institution. They cared about the vagaries of floating-point money only slightly more than they cared about numerical stability --- which is to say not at all.

</comment><comment><USER58> "if you are doing some financial math that does not need to be accurate to the penny, just use floating point numbers. If it is good enough for Excel, it will be good enough for most applications".

Uh, no.

</comment><comment><USER59> I just use longs with the value representing the number of pennies.

</comment><comment><USER60> You might get unexpectedly negative financial results if you don't use floored division and modulo properly:

[https://stackoverflow.com/questions/4467539/javascript- modul...](https://stackoverflow.com/questions/4467539/javascript-modulo-gives- a-negative-result-for-negative-numbers)

>JavaScript % (modulo) gives a negative result for negative numbers

>Question: According to Google Calculator (-13) % 64 is 51. According to Javascript (see this JSBin) it is -13. How do I fix this?

>Solution: ((i % n) + n) % n

The Forth-83 standard word /MOD (or FM/MOD) implemented floored division properly, subtly breaking many old FORTH programs, but it was a step (or rather a truncation) in the right direction, towards negative infinity instead of zero.

[https://www.nimblemachines.com/symmetric-division- considered...](https://www.nimblemachines.com/symmetric-division-considered- harmful/)

>Symmetric division considered harmful

>Since its 1983 standard (Forth-83), Forth has implemented floored division as standard. Interestingly, almost all processor architectures natively implement symmetric division.

>What is the difference between the two types? In floored division, the quotient is truncated toward minus infinity (remember, this is integer division we're talking about). In symmetric division, the quotient is truncated toward zero, which means that depending on the sign of the dividend, the quotient can be truncated in different directions. This is the source of its evil.

[https://forth-standard.org/standard/core/FMDivMOD](https://forth- standard.org/standard/core/FMDivMOD)

>Rationale:

>By introducing the requirement for "floored" division, Forth 83 produced much controversy and concern on the part of those who preferred the more common practice followed in other languages of implementing division according to the behavior of the host CPU, which is most often symmetric (rounded toward zero). In attempting to find a compromise position, this standard provides primitives for both common varieties, floored and symmetric (see SM/REM). FM/MOD is the floored version.

>The committee considered providing two complete sets of explicitly named division operators, and declined to do so on the grounds that this would unduly enlarge and complicate the standard. Instead, implementors may define the normal division words in terms of either FM/MOD or SM/REM providing they document their choice. People wishing to have explicitly named sets of operators are encouraged to do so. FM/MOD may be used, for example, to define:

        : /_MOD ( n1 n2 -- n3 n4) >R S>D R> FM/MOD ;

        : /_ ( n1 n2 -- n3) /_MOD SWAP DROP ;

        : _MOD ( n1 n2 -- n3) /_MOD DROP ;

        : */_MOD ( n1 n2 n3 -- n4 n5) >R M* R> FM/MOD ;

        : */_ ( n1 n2 n3 -- n4 ) */_MOD SWAP DROP ;</comment>


Losing Chess has been solved - <AUTHOR> http://magma.maths.usyd.edu.au/~watkins/LOSING_CHESS/ <comment><AUTHOR> ... and White win with 1. e3</comment>


TV Sports Online Watch Saracens v South Africa Rugby TV Online on PC 17-11-2009 - <AUTHOR> http://tvsportsonlinelive.blogspot.com/2009/11/watch-saracens-v-south-africa-live.html <comment><AUTHOR> Click Here To Watch Slovenia v Russia Live Playoffs World Cup Football On PC On Nov 18 19:45 UK</comment>


Wall Street Journal Reporter Forced to Leave China - <AUTHOR> https://www.wsj.com/articles/wall-street-journal-reporter-forced-to-leave-china-11567161852?mod=rsswn <comment><USER2> He'd have been charged with "inciting subversion of state power" if he holds Chinese passport.

The WSJ Chinese weibo account was banned last month, even though it basically only posts good news about China and bad news about America.

Also:

>The Chinese central government rejected Lam's proposal to withdraw the extradition bill and ordered her not to yield to any of the protesters' other demands at that time,

[https://www.reuters.com/article/us-hongkong-protests- china-e...](https://www.reuters.com/article/us-hongkong-protests-china- exclusive/exclusive-amid-crisis-china-rejected-hong-kong-plan-to-appease- protesters-sources-idUSKCN1VK0H6)

<comment><USER3> Is Reuters a credible news source? I have seen many news from that site but don't have a good way to verify their reports...

</comment></comment><comment><USER4> Somewhat related (topically, not necessarily causally), I've seen a number of tweets about journalists being hassled entering the US. So far none have actually been barred as far as I'm aware, but it seems the "fake news media" rhetoric from the top has been seared into (some of) the rank-and-file.

One example: [https://mobile.twitter.com/jamescdyer/status/116464700893166...](https://mobile.twitter.com/jamescdyer/status/1164647008931667971?ref_src=twsrc%5Etfw%7Ctwcamp%5Etweetembed&ref_url=https%3A%2F%2Fd-8158549391615904925.ampproject.net%2F1908231648370%2Fframe.html)

</comment><comment><USER5> It is an interesting point of comparison that the President of the United States, Donald Trump, considers meanstream news sources like the New York Times to be extremely malicious against him and unfair to him in their coverage.

The amount of censorship he applies to the New York Times is exactly 0.</comment>