The Identification Number and Password fields need to be filled in. Moreover, you cannot send messages devoid of contents Adding further data is not allowed because this topic has been closed Print You provided no password: you have to write it in the textfield beside the command delete for the topic you want to delete Are you sure you want to delete this comment? You did not cast a vote. You need to cast a vote by selecting a radio button. Impossible to proceed Provide the project number Insufficient form parameters: you forgot to fill in some field/s
 
member picmember pic
 
Snippets of A
 
info Below you can find the text of the snippet you want to read, and the list of the other snippets by this author if available.
What are snippets?
Share on MySpace

Javascript Cast And Recast Numerical Value Of Numerical Range Into Another Range

Snippet: Identification Number »   Snippet: Inclusion syntax »
Visitors: 32,638 Tagged by its author as: Programming Javascript Characters (in origin): 8,791 (pages: ~ 3)
Author: Em@il Permalink Cast your vote for this topic Printable version
OBJECTIVE: cast an entity into another entity after a different metrical system.Note: this file is about casting meant in its wider conceptual meaning.  
If you are here just to know how to cast (convert) a string into a number in javascript, all you have to do is to use parseInt or (to allow decimal fractions) parseFloat:  
 
var toNum = parseFloat(STRING);  
 
If STRING wasn't a number or a literal number (a number in between quotes), it will return NaN, a specific type of returned value (tantamount to false, and any conditional check would behave as if considering it such) which anyway still belongs, strangely enough, to the Number data type.  
 
To cast a Number into a String, prepend to it any string, also an empty one:  
vat intoString = "" + 351;  
 
Some even look, apparently, for a Javascript procedure to cast a number into a boolean. A possible way:  
var num=0;//or 7 or whatever number  
num=!!num;  
alert(num);
DETAILS: In many programming languages (so called strongly typed * languages such as C * ), numbers must be declared in their signatures * as belonging within a specified range (data type * ) before being allocated: that is, before writing down the number assigned to something, you have to declare in its signature within which range such number is to fall/pertain.  
So, data types may also be thought of as ranges in disguise: the conventional names assigned to different spans of ranges.  
 
As a matter of fact, typical ranges of signed/unsigned numerical data types are:
byte: numbers within -128 +127  
So range span is 256 (including zero).
short: numbers within -32,768 +32,767  
So range span is 65,536 (including zero).
integer: numbers within -2,147,483,648 +2,147,483,647  
So range span is 4,294,967,296 (including zero).
long: numbers within -9223372036854775808L +9223372036854775807L
Now, if a number which has ben assigned say a long type needs to be reduced to a shorter type so to occupy less memory (or, indeed, as a bug * overlooked in a situation where inputs weren't expected to belong to the passed data type, and yet they do), this is the operation whose name is: type casting.  
Basically it means translating the number that belongs to the wider range, in our example, to a number that belongs to a smaller range.  
 
This transformation implies a dramatic mutation of the number: in fact if you were to cast from a smaller range to an higher range, you won't have problems because the higher range can accommodate the smaller; but when you perform the other way round, the number that was originally in the wider range won't fit the smaller range: the only way then to represent this bigger number into the smaller range, is to curtail it recursively by as many rounds are needed until it finally falls for the first time within the desired smaller range: exhausted the rounds, the offset within the smaller range where such number ends up without further margin, is its cast version.  
 
In this prospect, remember that in math a division is just a recursive subtraction performed throughout rounds until the outcome is attained, exactly in the same fashion a multiplication is just a recursive sum performed as many rounds as the factors (inputs) prescribe.  
 
This process after which a wider unit is gradually (recursively, we'd say nowadays) shrunk until it fits a smaller range was called by the ancient Greeks antanairesis and is, actually, the quintessence of all known measurement logics.  
function cast(value, range, noRound){
//Validate:
value=parseFloat(value);
if(isNaN(value)){return false;};
range=parseFloat(range)||1;
//Run:
var division=""+(value/range);
var integer=(division.indexOf(".")>=0)?
	division.substring(0, division.indexOf(".")):division;/*rounds outside range: useful only to recast*/
var fractional=(division.indexOf(".")>=0)?
	"0."+division.substring(division.indexOf(".")+1):0;/*rounds within range: useful to cast*/
var reciprocal=1/range;
value=parseFloat(fractional)/reciprocal;
return [ (!noRound)?Math.round(value):value, integer, range ];
/* keep this comment to reuse freely:
http://www.fullposter.com/?1 */}
Remove colors  
INPUTS: first the number to cast. Second input argument the range into which to cast. An optional third argument if passed as 1 returns the values without rounding (that is, if fractional parts are present, they are preserved).  
 
RETURNS: boolean false if the first input argument was not a number, or an array of three entries:
  1. 0: the yielded value
  2. 1: the amount of rounds performed outside the final range where the offset was located.
  3. 2: the input range argument, unmodified.
Instance:  
cast(300, 256); //means: translate 300 given a range of just 256  
//returns:  
 
array(44 , 1, 256)
 
 
CAVEATS: proceedings of the functions:  
 
About the performed operation value/range: it finds how many rounds the range falls within the scope of value (in the simplest setting the value exceeds the range).  
 
From the above division are drawn:  
Its integer part (that is, before any fractional part): this represents how many rounds the division has performed before the value for the first time finds the range.  
Its fractional part: this represents, once fallen within the range, how many steps have been made inside the last range to find the final offset.  
 
About the performed operation reciprocal=1/range: do not get puzzled, this simply expresses the range in another way: as fraction of the unit, so to fit the unit of measure (number 1) of the fractional value yielded above (all fractional values, in fact, are fractions insofar as they are inferior to the unit): example if range is 3 => (0.3333333 , 0.3333333 , 0.3333333)  
 
About the performed operation fractional/reciprocal: means how many times such (reciprocal) fraction (0.3333333) is found within the fractional part namely within the rounds performed inside range: we have cast.  
OBJECTIVE: perform the reverse process than above.  
function recast(value, integer, range){
//Validate:
if(typeof(value)=="object" && value.length){
integer=value[1];
range=value[2];
value=value[0];
};
value=parseFloat(value);
integer=Math.round(parseFloat(integer));
range=parseFloat(range);
if(isNaN(value) || isNaN(integer) || isNaN(range)){return false;};
//Run:
return (range*integer/*rounds made*/)+value;
/* keep this comment to reuse freely:
http://www.fullposter.com/?1 */}
Remove colors  
INPUTS: three arguments, each representing exactly the items returned by the previous function (see above what it returns).  
 
RETURNS: boolean false if the input arguments weren't numbers, or a number.  
 
CAVEATS: this is a hint more than a caveat: since the functions accept numbers as input arguments, nothing precludes the possibility that these numbers could be numerical indexes of an array.  
If then the values of the array pointed by those numerical indexes were something else than numbers, you may retrieve such values using the returned number - and therefore perform casting even on alphabets or strings!  
Instance:  
//prepare inputs:
var alpha="abcdefghijklmnopqrstuvwxyz"; /*bigger range from which to get...*/
var letterToCast="s"; /*...an item to cast into the...*/
var rangeTocast="abcdx"; /*...smaller range.*/

//run:
var foo=cast( alpha.indexOf(letterToCast), rangeTocast.length );

//inspect returned values:
alert(foo); /*that prints: (3, 3, 5): letter s in the range "abcdefghijklmnopqrstuvwxyz" cast into an alphabet range as "abcdx" or whatever: values are immaterial once they are univocally identified by numerical indexes*/
alert( rangeTocast.charAt(foo[0]) ); /*from the index to the string: returns 'd': letter 's' is cast in letter 'd' in the given settings*/

//recast:
alert(
alpha.charAt( recast(foo[0], foo[1], foo[2]) )
);
Remove colors
Printable version To exclude hyperlinks from the print, check the checkbox »
Rss

Cast your vote for this topic

To perform this operation it is not necessary to be Full Poster members.
«Negative Positive»
Click here to save your vote (Current average: 4.00, Voters: 4)
Are you the author of this topic and do you want to append quickly more text? click here
Other topics
This subscriber has a blog too: Read the blog: 57 Shoutbox: 103 reviews: 153
Visitors: 32,638
Overall visits to all the topics: 4,983,219
Daily average (Calculated from the website subscription day): 2,066.01
Optional sorting commands:
Normal order: click here
Order by amount of visits: click here
Order by category: Programming Javascript
Current order: normal
Other categories available for this author (Limited data report: 100):
Advice: Martial Arts and Self Defense(4), Books(236), Critical Reviews and Essays(4), Dictionaries(1), Emergency Care(4), Epistles Letters and Advice(5), Fantasy Epics and Fables(1), History and Documents(5), Humor and Jokes(2), Methematics(2), Music and Lyrics(6), News Digests and Press Reviews(1), Novels Poetry and Stories(3), Philosophy Reviews(7), Poetry(1), Programming(4), Programming Ajax(2), Programming Javascript(82), Programming Php(52), Psychology(1), Quotes(1), Religion Esoterica and Spirituality(12), Scientifical Reviews(4), Self Improvement(1), Sport Activities and Apparels(2), Tarots(1)
Showing topics: 1, 10
Available total: 448
View only a list of the snippets by this author: click here.
Other topics available for this author: click on any title below to view the complete item:
Fighting Competently: Anticipation, And Remember It's In His Eyes Identification Number: 465 Visitors: 446 La Musa Segreta: Superiorità Onnipervasiva Della Boxe Identification Number: 464 Visitors: 1,262 Ha Senso Il Doping Sportivo? Effetto Matteo Nello Sport E Business Sportivo Identification Number: 463 Visitors: 1,465 The Musicians Within The Music Box And Other Hereafter Stories Identification Number: 462 Visitors: 2,725 Freud And Jung In A Nutshell: Three Or So Shots At Psychoanalysis For Dummies Identification Number: 461 Visitors: 4,183 Division The Math Of Gods: Ambiguities Of Antanairesis And New Math Operations Identification Number: 460 Visitors: 4,490 The Meaning Of Cruelty Identification Number: 459 Visitors: 4,892 Dÿanèra Ad Eleusi: La Folgorazione Ontologica: Il Pensare Sistematico E Non Identification Number: 458 Visitors: 5,645 Creative Writing: How To Write A Novel. Best Tips From The Bester Professionals Identification Number: 457 Visitors: 7,537 Newsreel Pseudo Intel: The Middle East Approaching The New 20s Identification Number: 456 Visitors: 7,485
External services
This page of this subscriber uses external services: Hide