I have a little Problem in Blitzmax.
I try to read an INI-file and if I read floats they are converted in a very strange way.
The line in the file which is concerned looks like that for example:
_fStrength=40.6
The Output of this looks like that:
DebugLog:_fStrength: 40.5999985
The code I use to read that works with reflection and looks like that:
For Local fld:TField = EachIn id.EnumFields()
fld.Set(obj, SearchInFile("TempWeapon" + index, fld.Name(), "Weapons.ini"))
DebugLog(fld.Name() + ": " + String(fld.Get(obj)))
Next
I found out, that this only happens if the number after the "." does not equal's 5 or 0.
I can't explain this behaviour, because if I do not use reflections, it works fine.
Could anyone help me please?
As you probably know, your computer stores numbers in binary code, using a limited size. 40.6 expanded in binary is a periodic sequence (101000.1001100110011001100..., infinitely), similarly to what happens when you try to write down the digits of 1/3) repeating and thus can not be represented exactly, so you get rounding errors.
The number of correct digits you get here looks like you are using single-precision floating point numbers, you can push the error further back by going to double, but it won't disappear.
As a reference, you might find Wikipedia on floating point helpful.
Related
i am trying to print variables with indices.
The goal is to write something like:
x_1 + x_2 = 3 + 1 = 4
The problem is, that variables just like x_1 does not show an index. It shows the underscore itself.
/* works fine */
print(x_1)$
x_1;
/* Does not work */
ostream: make_string_output_stream()$
printf(ostream, string(x_1))$
get_output_stream_string(ostream);
Output of the code above
Converting "x_1" into a string destroys the underscore, but you need a string for the method printf().
Are there workarounds for this?
What to do here probably depends somewhat on what goal you need to achieve with this. Can you say more about the larger goal you are working toward?
In order to generate output which has typesetting instructions in it, you can call the tex or tex1 function to generate TeX output. If that needs to be displayed in a web page, I believe you can make use of the MathJax Javascript library. Sorry, I don't know more about MathJax.
You should probably write x[1] instead of x_1. Displaying x_1 with a subscript 1 is a convenience -- the rest of Maxima doesn't know much about it.
EDIT: There is also an add-on package to output MathML; there might be two such packages, I would have to check. If MathML could help solve your problem, I will look into it.
I understand that the string_variable(start:length) can be used to get a substring of a string given a starting point and substring length, however, I am finding that I often need to get a substring between a 'start' and 'end' point.
While I know I could always do this:
SUBTRACT start FROM end GIVING len
string(start:len)
It seems cumbersome to have to do so every time when I am writing programs that use this functionality often. Is there perhaps a quicker/built-in way of achieving this?
How about?
move str (start-pos : end-pos - start-pos + 1) to ...
You can subtract the first from the last, but you need to add 1 to get the correct length.
STRING is a statement name, as is START, and END is reserved. LENGTH is a function name. I avoid those in anything that looks like code.
i just have a quick question on this bit of code i have here on the Cryptopals Challenges, using Python3, For XORing a string with one single character.The program takes in a hex string string, decodes it, XORs it with a single character, does this for every possible character, then finds the "most english" line of XORd data. heres my code snippet (which i admittdly used from a solutions page ) :
def singlechar_xor(input_bytes, key_value):
"""XORs every byte of the input with the given key_value and returns the result."""
output = b''
for char in input_bytes:
output += bytes([char ^ key_value])
return output
i know what is happening and i understand what is supposed to happen, im just not sure how bytes behave and what types are and arent supposed to be XORd. Why do i need the brackets around char^key_value? If i remove the brackets my output becomes a bunch of 0's. What is the result of the XOR or the character and the key_value? If someone could kindly explain so i could have a better understanding going forward in these challenges id GREATLY appreciate it <3
I've been looking all around the web for this answer and can't find it.
I'm semi new to python so hoping i get an answer here,
so basically what i want to do is, access a text file "number.txt"
that has a 10 as a line, and do a sum within the python code.
Here is what i got so far:
with open('number.txt', 'r') as sum:
num = sum.readline()
clean = num.rstrip('\n')
#number.txt file only contains 1 line and is a 10
increase = "5"
adding = clean + increase
print(adding)
it doesn't do the sum, instead i get the 5 added after the 10
so instead of getting 15 i get 105.
can anyone help?
Welcome to programming. Keep aware of the concepts of data types, I suggest doing some reading on that.
The + operator behaves differently between strings, arrays, and integers.
I would give you the answer but I am guess you are doing this as an assignment of sorts, so I just wanted to point you in the right direction. Use a python debugger (like PyCharm, or Wing IDE) to determine the differences between "5" + "10" 5+10 and [5]+[10]
You need to look into what's called Type Casting
I have a rectangular Coordinate in Matlab that looks like the following:
0.0240 - 0.1680i
I'd like to split the double into it real and imaginary parts, those parts being 0.0240 and -0.1680 (Don't need the i here)
I've converted the double into a string using the following:
I=0.0240 - 0.1680*i
I_1=num2str(I)
Im not sure how to proceed here to get what i want. strsplit() just gives back the string in the form it already is. Id like to somehow split it to give me the two numbers separately. I'm not too experienced with data manipulation in Matlab so any help is appreciated.
num2str converts number to string. It is not for separating real and imaginary parts.
You can use:
I=0.0240 - 0.1680*i;
real_part=real (I)
imaginary_part=imag(I)