littlepinkpie Posted November 30, 2017 Share Posted November 30, 2017 Hi, this is an easy question for someone with average knowledge of scripting. Im making a simple division float Division=9/2; llOwnerSay((string)Division); Im getting back 4.000000 instead of 4.5. What am I doing wrong? Link to comment Share on other sites More sharing options...
Innula Zenovka Posted November 30, 2017 Share Posted November 30, 2017 You are dividing two integers, which returns an integer. You are then casting that integer as a float. Try float Division = 9.0/2.0; Link to comment Share on other sites More sharing options...
Parhelion Palou Posted November 30, 2017 Share Posted November 30, 2017 (edited) By using 9 and 2 rather than 9.0 and 2.0, you're using integers rather than floats. The system is doing an integer divide of 9/2, which is equal to 4, then casting it to a float: 4.00000 Pretty close to simultaneous with Innula Edited November 30, 2017 by Parhelion Palou Match! Link to comment Share on other sites More sharing options...
littlepinkpie Posted November 30, 2017 Author Share Posted November 30, 2017 I have a dynamic list and I need to divide its length with an integer Currently the list length is 9 and my integer is 2 So Im using float Result; MyList=llGetListLength(List); Result=float(MyList)/float(MyInteger); llOwnerSay((string)Result); where MyList=9 and MyInteger=2 but Im getting syntax error at Result= Link to comment Share on other sites More sharing options...
Rolig Loon Posted November 30, 2017 Share Posted November 30, 2017 Bad syntax. It should be float Result = (float)MyList / (float) MyInteger; Link to comment Share on other sites More sharing options...
littlepinkpie Posted November 30, 2017 Author Share Posted November 30, 2017 Im declaring it: float Result; Link to comment Share on other sites More sharing options...
Rolig Loon Posted November 30, 2017 Share Posted November 30, 2017 (edited) As you should, assuming that you intend it to be a global float. Edited November 30, 2017 by Rolig Loon Link to comment Share on other sites More sharing options...
littlepinkpie Posted November 30, 2017 Author Share Posted November 30, 2017 But still it says "Syntax error" Link to comment Share on other sites More sharing options...
Madelaine McMasters Posted November 30, 2017 Share Posted November 30, 2017 (edited) When you want the compiler to temporarily treat one kind of variable as another, you use a "typecast". Typecasts are always enclosed in parentheses. So, if you want to temporarily express the integer variable "MyList" as a float, you write "(float)MyList". You wrote float(MyList), which the compiler interprets as a function call to the routine "float" with the argument "MyList". There is no such function, and so you get a syntax error. The correct way to write it is as Rolig showed. "(float)MyList/(float)MyInteger". Edited November 30, 2017 by Madelaine McMasters Link to comment Share on other sites More sharing options...
Rolig Loon Posted November 30, 2017 Share Posted November 30, 2017 1 minute ago, littlepinkpie said: But still it says "Syntax error" Then you must have another error somewhere else in the script. Or, read Maddy's note. ^^ Link to comment Share on other sites More sharing options...
littlepinkpie Posted November 30, 2017 Author Share Posted November 30, 2017 Ok my bad, I missed something there, its working now. Thank you all! Link to comment Share on other sites More sharing options...
Recommended Posts
Please take a moment to consider if this thread is worth bumping.
Please sign in to comment
You will be able to leave a comment after signing in
Sign In Now