How to understand `set_fixed_point_multiplier`

正在查看此主题的用户

DoDoCat

Squire
I have some code like this:

插入代码块:
assign, ":value_2", 200
set_fixed_point_multiplier, 1000
assign, ":value_1", 9800
assign, reg0, ":value_1"
display_message, "@{reg0}"
assign, reg0, ":value_2"
display_message, "@{reg0}"

The output is 98000 and 2000

Here is my idea about `set_fixed_point_multiplier`
插入代码块:
fixed point declared 1000

defined a value with 1000

real value = declared value / fixed point = 1000 / 1000 = 1
output value  = declared value  = 1000

Why?
 
DoDoCat 说:

you are moving the "," / "." one character/number/house/spot/location to the right.

it allows you to use integer math with non integer values.

you cant do stuff with 12,5 or 12.5

but you can do stuff with 125

插入代码块:
################################################################################
# [ Z03 ] MATHEMATICAL OPERATIONS
################################################################################

  # Mathematical operations deal with numbers. Warband Module System can only
  # deal with integers. Floating point numbers are emulated by the so-called
  # "fixed point numbers". Wherever you encounter a fixed point parameter for
  # some Module System operation, keep in mind that it is actually just a
  # regular integer number, HOWEVER it is supposed to represent a floating
  # point number equal to fixed_point_number / fixed_point_multiplier. As you
  # might have guessed, to convert a floating point number to fixed point, you
  # have to multiply it by fixed_point_multiplier. You can change the value of
  # multiplier with the operation (set_fixed_point_multiplier), thus influencing
  # the precision of all operations dealing with fixed point numbers.

  # A notion very important for Warband modding is that you reference all
  # Warband objects by their numeric values. In other words, you can do maths
  # with your items, troops, agents, scenes, parties et cetera. This is used
  # extensively in the code, so don't be surprised to see code looking like
  # (store_add, ":value", "itm_pike", 4). This code is just calculating a
  # reference to an item which is located 4 positions after "itm_pike" inside
  # the module_items.py file.
 
kalarhan 说:
you are moving the "," / "." one character/number/house/spot/location to the right.

it allows you to use integer math with non integer values.

you cant do stuff with 12,5 or 12.5

but you can do stuff with 125
But why can this operation effect the value before the `set_fixed_point_multiplier`?

I defined a value before I calling set_fixed_point_multiplier
but the value still was changed by set_fixed_point_multiplier

assign, ":value_2", 200
set_fixed_point_multiplier, 1000
assign, reg0, ":value_2"
display_message, "@{reg0}"

the output value is 2000, it was effected by the set_fixed_point_multiplier
 
DoDoCat 说:
But why can this operation effect the value before the `set_fixed_point_multiplier`?

scope. So you can do math operations at any time in your current scope. Also you can change the multiplier back and forth inside that same scope, but remember you will lose information

there is no problem doing this: 200.0 => 2000.0. You are not losing information by going to the right. However, the same doesnt apply if you move to the left (think 2005.0, what happens if you divide/reduce the multiplier).

200 * 10 = 2000 / 10

2005 / 10 != 200 * 10
 
kalarhan 说:
DoDoCat 说:
But why can this operation effect the value before the `set_fixed_point_multiplier`?

scope. So you can do math operations at any time in your current scope. Also you can change the multiplier back and forth inside that same scope, but remember you will lose information

there is no problem doing this: 200.0 => 2000.0. You are not losing information by going to the right. However, the same doesnt apply if you move to the left (think 2005.0, what happens if you divide/reduce the multiplier).

200 * 10 = 2000 / 10

2005 / 10 != 200 * 10
I finally found why the value is 2000, because I use a custom output script, and I didn't send the second parameter, so when the script output the message, it will add a `0` to the end of the value, so the real value is 200, not 2000 :smile:
 
kalarhan 说:
DoDoCat 说:

you are moving the "," / "." one character/number/house/spot/location to the right.

it allows you to use integer math with non integer values.

you cant do stuff with 12,5 or 12.5

but you can do stuff with 125

Okay,

If I declared a fixed pointer with 100, and declared a value with 125, so true value is 1.25, right?

And I use store_cos to calcualte this value's cos lke this:
store_cos, ":cos_value", 125

equals

store_cos, ":cos_value", 1.25

and the return true value is ":cos_value" / 100 (if fixed pointer is 100)?
 
后退
顶部 底部