Rust decimal to roman numeral kata

Just wrote this decimal to roman numeral converter, inspired Kevlin Henney’s talk “Get Kata”.

I have been reading the rust doc’s little by little and this is one of my first attempts at some code.

I’d like to get feedback on style, general rust idioms and how I could have implemented this algorithm in more succinct manner.

code also available here

use std::env;
use std::io;
use std::io::BufWriter;
use std::io::Write;

static BASE10_NUMERALS: (&str; 7) = ("I", "X", "C", "M", "X̄", "C̄", "M̄");

static CENTRE_NUMERALS: (&str; 6) = ("V", "L", "D", "V̄", "L̄", "D̄");

struct Bases {
    base: usize
}


impl Iterator for Bases {
    type Item = usize;

    fn next(&mut self) -> Option<usize> {
        self.base = self.base - 1;
        Some(self.base)
    }
}

fn iter_bases(largest_base: usize) -> Bases {
    Bases { base: largest_base }
}


fn encode((decimal_number, base, ): (char, usize, )) -> String {
    let digit = decimal_number.to_digit(10).unwrap();
    let max_base = CENTRE_NUMERALS.len();

    if base >= max_base {
        BASE10_NUMERALS(BASE10_NUMERALS.len() - 1)
            //This pow function is the main limiter for decimal size
            .repeat((10_u32.pow((base - max_base) as u32) * digit) as usize)
    } else {
        if digit == 9 {
            format!("{}{}"
                    , if base == 3 { "Ī"} else { BASE10_NUMERALS(base) }
                    , BASE10_NUMERALS(base + 1)
            )
        } else if digit >= 5 {
            format!("{}{}", CENTRE_NUMERALS(
                base), BASE10_NUMERALS(base)
                        .repeat((digit - 5) as usize))
        } else if digit == 4 {
            format!("{}{}", BASE10_NUMERALS(base), CENTRE_NUMERALS(base))
        } else {
            // Less than 4
            BASE10_NUMERALS(base).repeat(digit as usize)
        }
    }
}


fn main() {
    let args: Vec<String> = env::args().collect();
    let mut writer = BufWriter::new(io::stdout());
    let input = &args(1);
    for roman_numeral in input
        .chars()
        .zip(iter_bases(input.len()))
        .map(encode) {
        writer.write(roman_numeral.as_bytes())
            .expect("Unable to write to stdout");
    }
}

syntax – Padding decimal output with ending zeros

I want to pad with ending zero and tried the following code

d = 7;
f[x_] := 150/x
g[x_] := 2 Log[x] - 3
Table[{x, StringPadRight[ToString[NumberForm[N[f[x]], d]], d, "0"], 
    StringPadRight[ToString[NumberForm[N[g[x]], d]], d, "0"]}, {x, 20,
     50}] // N // TableForm

which gave

 20. 7.50000 2.99146
 21. 7.14285 3.08904
 22. 6.81818 3.18208
 23. 6.52173 3.27098
 24. 6.25000 3.35610
 25. 6.00000 3.43775

which is kind of ‘OK’.

Are there any better/shorter code to get this result?

Also, is there a simple way to remove the period for the integers in the first column?

animator – How do I set decimal values in 1D blend tree?

I have created a 1D blend tree.
It is being used for a 3rd Person Controller.

The blend tree consists of 3 animations:

  1. aim pistol down to the floor
  2. aim pistol at middle of the screen (straight forward)
  3. aim pistol up in the sky

enter image description here

To determine the blend value (which animation should be played), I inspect the current mouse position and convert it a value between 0 (aim low) and 1 (aim high). The middle position would be 0.5.
Here is my code:

float f = Input.mousePosition.y / Screen.height;

Debug.Log("Blendval " + f.ToString());//
_animator.SetFloat("BlendVal", f);

I tried to insert these decimal values in the blend tree like this:

enter image description here

However, this doesn’t work.
Unity converts my “0.5” to 0.
I don’t see why.

I can instead use integers like this:

enter image description here

Why doesn’t Unity allow me to use 0, 0.5 and 1?
Why do I have to use “other” values like -1, 0 and 1?

Thank you!

bugs – NearestNeighborGraph doesn’t show a 3D graph when one or more points are Integer and the rest are Decimal?

Bug introduced in 10.3 or earlier and persisting through 12.1.1


When all the points are the same kind, say Integer then NearestNeighborGraph will display a 3D graph when using {x,y,z} coordinates. However, when just one of those points is say a Decimal instead then one sees a 2D graph. It seems the reverse is true too. For example:

allIntegers = NearestNeighborGraph[{{0,0,0},{-1,1,1},{1,-1,1},{-1,-1,1},{1,1,1},{0,0,2},{0,0,1}},{All,2}];
allDecimals = NearestNeighborGraph[{{0.,0.,0.},{-1.,1.,1.},{1.,-1.,1.},{-1.,-1.,1.},{1.,1.,1.},{0.,0.,2.},{0.,0.,1.}},{All,2}];

oneInteger = NearestNeighborGraph[{{0,0.,0.},{-1.,1.,1.},{1.,-1.,1.},{-1.,-1.,1.},{1.,1.,1.},{0.,0.,2.},{0.,0.,1.}},{All,2}];
oneDecimal = NearestNeighborGraph[{{0.,0,0},{-1,1,1},{1,-1,1},{-1,-1,1},{1,1,1},{0,0,2},{0,0,1}},{All,2}];

Row[{allIntegers, allDecimals, oneInteger, oneDecimal}]

Displays:

2 3D graphs and 2 2D graphs

Is there something I’m doing wrong or some parameter I need to pass? I’d like to be able to force 3D.

hive – Bit And Operation in Decimal Column in Impala

Consider in impala, I have a decimal column, is there a way to convert to binary using bin function

I can see clearly that bin is accepting only big int argument not the decimal

So, if there is a number like 122642280288770432172031 which is crossing the big int limit in impala, how do I convert it to binary form and use a bit and(https://impala.apache.org/docs/build/html/topics/impala_bit_functions.html#bit_functions__bitand) operation on it?

Also, let us know if there is a possibility in Hive if not for impala

PHP Math calculations creating 64 bit double instead of 2 digit decimal places

I have a very simple code in PHP doing some math calculations

$mTotalPrice   = ($totalPrice * 100)/100;
    $totalTaxValue = ($totalTaxValue * 100)/100;
    $rewardsData('discountValue') = ($rewardsData('discountValue') * 100)/100;
    $rewardsData('discountTax')   = ($rewardsData('discountTax') * 100)/100;

    $totalPrice    = round($mTotalPrice, 2) - round($rewardsData('discountValue'), 2) - round($rewardsData('discountTax'), 2);
    $totalTaxValue = $totalTaxValue - $rewardsData('discountTax');
    $totalPrice    = $totalPrice * 100;
    $totalTaxValue = $totalTaxValue * 100;

The result of these values are shown in the JSON

{
        "mTotalPrice": 6.44000000000000039079850466805510222911834716796875,
        "rewards": {
            "discountTax": 0.4899999999999999911182158029987476766109466552734375,
            "discountValue": 5.95000000000000017763568394002504646778106689453125,
        },
        "totalPrice": 2.220446049250313080847263336181640625e-14,
        "totalTaxValue": 0
    }

Now Ideally these are prices so I want them to be converted to 2 digit values only but rounding doesn’t work in the above code at all.

So if I have a number 49 and I divide it by 100 I get 0.4899999999999999911182158029987476766109466552734375

How can I maintain low number of decimal places