Discuss Scratch
- Discussion Forums
 - » Bugs and Glitches
 - » Variable glitch -- Scratch converts "Infinity" items to zero
        
         
- 960times10_5is106044
 - 
                            
						
						
                            Scratcher
                        
						
						 
500+ posts
Variable glitch -- Scratch converts "Infinity" items to zero
My browser / operating system: Windows NT 10.0, Chrome 99.0.4844.51, No Flash version detected
There is a super weird glitch that you can replicate without any browser extensions:
                        
                        
                    There is a super weird glitch that you can replicate without any browser extensions:
-  Create a new project here.
 -  Create a new variable (name it “bug”), with any value you wanted.
 -  Add this code:
set [bug v] to ((1) / (0)) // returns infinity
 - Save the project, and then reload the page.
 -  Look at the variable and it says zero! What a glitch!
 
add ((1) / (0)) to [list v]
- RL1123
 - 
                            
						
						
                            Scratcher
                        
						
						 
1000+ posts
Variable glitch -- Scratch converts "Infinity" items to zero
This may happen because of the Divide by Zero error. I hope you know that 1/0 does not actually equal infinity, so I bet the editor just puts it there to give a placeholder instead of an error. Then, when you save the project, it'll reset it to zero because 1/0 isn't a value.
Edit: This is wrong, sorry for the misinformation, see posts below
                        
                            Edit: This is wrong, sorry for the misinformation, see posts below
Last edited by RL1123 (March 11, 2022 23:44:50)
- Flowermanvista
 - 
                            
						
						
                            Scratcher
                        
						
						 
1000+ posts
Variable glitch -- Scratch converts "Infinity" items to zero
My browser / operating system: Windows NT 10.0, Firefox 98.0, No Flash version detected
I'd also like to note that, while not necessarily mathematically correct, the IEEE 754 Standard for Floating-Point Arithmetic (on which nearly all modern implementations of floating point numbers are based) stipulates that division by 0 should return Infinity.
As another side note, this same behavior also applies to NaN - it also gets converted to 0 when saving and reloading a project.
EDIT: (kind of edit-ninja'd?) The rabbit hole goes deeper - this is intentional, as I suspected. The culprit is lines 42-61 of scratch-vm/src/util/string-util.js:
By default, the built-in function JSON.stringify() will convert Infinity and NaN values into null, because those values are all considered to be null, for some reason (source). If anyone could enlighten me as to why it does this, that would be greatly appreciated, because I don't get it.
                        
                            I bet the editor just puts it there to give a placeholder instead of an error. Then, when you save the project, it'll reset it to zero because 1/0 isn't a value.This isn't correct, because other methods of creating Infinity also exhibit the same behavior. For example, creating a number that is larger than ~1.8e308 (such as by multiplying two very large numbers) will also create Infinity, and that will also turn into 0 when the project is saved and reloaded. I can't really think of a reason for this to happen, since Infinity is a perfectly valid floating point number that doesn't cause any issues (although it's not exactly useful, attempting to do any operations on it either returns Infinity, -Infinity, or NaN).
I'd also like to note that, while not necessarily mathematically correct, the IEEE 754 Standard for Floating-Point Arithmetic (on which nearly all modern implementations of floating point numbers are based) stipulates that division by 0 should return Infinity.
As another side note, this same behavior also applies to NaN - it also gets converted to 0 when saving and reloading a project.
EDIT: (kind of edit-ninja'd?) The rabbit hole goes deeper - this is intentional, as I suspected. The culprit is lines 42-61 of scratch-vm/src/util/string-util.js:
/** * A customized version of JSON.stringify that sets Infinity/NaN to 0, * instead of the default (null). * Needed because null is not of type number, but Infinity/NaN are, which * can lead to serialization producing JSON that isn't valid based on the parser schema. * It is also consistent with the behavior of saving 2.0 projects. * This is only needed when stringifying an object for saving. * * @param {!object} obj - The object to serialize * @return {!string} The JSON.stringified string with Infinity/NaN replaced with 0 */ static stringify (obj) { return JSON.stringify(obj, (_key, value) => { if (typeof value === 'number' && (value === Infinity || value === -Infinity || isNaN(value))){ return 0; } return value; }); }
Last edited by Flowermanvista (March 11, 2022 20:36:52)
- Geotale
 - 
                            
						
						
                            Scratcher
                        
						
						 
100+ posts
Variable glitch -- Scratch converts "Infinity" items to zero
This seems to be a side effect of JavaScript's JSON.stringify function converting NaN and Infinity to null.
You can test this with the following:
I personally don't know how this could ever be fixed with how the JSON stores variables and lists. Specifically, it stores these as numbers or strings in a different way (using quotation marks), not storing which is which separately from its value, unlike block inputs, and as certain operations rely on whether a value is a number or a string (for example, the “set costume” block), this means that you could create a project that breaks because of this.
                        
                        
                    You can test this with the following:
> JSON.stringify({a:NaN}) < '{"a":null}' > JSON.stringify({a:Infinity}) < '{"a":null}'
- mybearworld
 - 
                            
						
						
                            Scratcher
                        
						
						 
1000+ posts
Variable glitch -- Scratch converts "Infinity" items to zero
This may happen because of the Divide by Zero error. I hope you know that 1/0 does not actually equal infinity, so I bet the editor just puts it there to give a placeholder instead of an error. Then, when you save the project, it'll reset it to zero because 1/0 isn't a value.

- soarroying
 - 
                            
						
						
                            Scratcher
                        
						
						 
500+ posts
Variable glitch -- Scratch converts "Infinity" items to zero
My browser / operating system: Windows NT 10.0, Chrome 99.0.4844.51, No Flash version detectedI understand your issue. However, I need more info (Browser glitches, macros, etc) so could you provide some?
There is a super weird glitch that you can replicate without any browser extensions:This happens on lists, too, whenever you put this code:
- Create a new project here.
 - Create a new variable (name it “bug”), with any value you wanted.
 - Add this code:
 set [bug v] to ((1) / (0)) // returns infinity- Save the project, and then reload the page.
 - Look at the variable and it says zero! What a glitch!
 add ((1) / (0)) to [list v]
Last edited by soarroying (March 12, 2022 00:45:59)
- medians
 - 
                            
						
						
                            Scratcher
                        
						
						 
1000+ posts
Variable glitch -- Scratch converts "Infinity" items to zero
This also happens with any number as the numerator. 0/0 turns out to be NaN, but that also turns into 0.
                        
                            I understand your issue. However, I need more info (Browser glitches, macros, etc) so could you provide some?Macros is completely unrelated, don't quote the original post, and there is enough information.
Last edited by medians (March 12, 2022 03:53:08)
- Discussion Forums
 - » Bugs and Glitches
 - 
            » Variable glitch -- Scratch converts "Infinity" items to zero 
         
            






